当我选择一行时,文本颜色变为白色。但是,当我滚动刷新该行时,文本颜色会变回黑色。所以我添加了一个逻辑if(cell.selected)。但是它没有用。当我滚动时。如果是大小写,则代码无法进入。这是代码:-

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let selectedCell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
    for subview in selectedCell.subviews {
        if let view = subview as? UILabel {
            view.textColor = UIColor.whiteColor()
        }
    }
 }

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var renderWidth:CGFloat = 8
    var renderYPosition:CGFloat = 15
    let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "idcell")
    let date = UILabel(frame: CGRectMake(renderWidth, renderYPosition, 110, 20))
    let dateText = "asdasdsad"
    date.text = dateText
    if (cell.selected) {
        date.textColor = UIColor.whiteColor()
    }
    cell.addSubview(date)
 }

最佳答案

您每次都在创建一个新的UITableViewCell。建议重用它。

与其创建一个新的单元格:

let cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "idcell")


使用它来重用单元格:

let cell = tableView.dequeueReusableCellWithIdentifier("cellIdInStoryboard", forIndexPath: indexPath) as! UITableViewCell


该标识符应与您在情节提要中设置的标识符相同。

关于ios - cell.selected不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39382180/

10-14 20:06