Using UITableViewDataSource with Custom Cells Having Subviews

前端 未结 3 1132
北荒
北荒 2021-01-17 00:57

When using a custom cell in a UITableView I am getting a strange table overlap:

Problem

  • scroll down & last two rows have top two r
3条回答
  •  自闭症患者
    2021-01-17 01:46

    The problem with cell identifier must be unique

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let  cellId:String = "Cell"+String(indexPath.row)
        var cell  = tableView.dequeueReusableCellWithIdentifier(cellId)
        if (cell == nil){
            cell = UITableViewCell(style: .Default, reuseIdentifier:cellId)
        }
        let cellText:String = String(indexPath.row)
        let subjectField:UILabel = UILabel(frame: CGRect(x:55, y: 25, width: 303, height:  25))
    
        subjectField.text = cellText
    
        (cell! as UITableViewCell).addSubview(subjectField)
    
        return cell! as UITableViewCell
    
    }
    

提交回复
热议问题