Using UITableViewDataSource with Custom Cells Having Subviews

前端 未结 3 1138
北荒
北荒 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:57

    Based on Rajesh Balam's Answer here is working form of my original code from my UITableViewDataSource!

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
        /**********************************************************/
        /* @soln  a UNIQUE 'cellId' is the key                    */
        /**********************************************************/
        let  cellId  :String = "Cell" + String(indexPath.row);
    
        var cell : UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellId);
    
        if (cell == nil){
            cell = UITableViewCell(style: .Default, reuseIdentifier:cellId); //comment this out and watch it fail!
        }
    
        let cellText:String = self.items[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;
    
    }
    

    It works. THANK YOU Rajesh!!! :)

提交回复
热议问题