使用带有子视图的自定义单元格的UITableViewDataSource [英] Using UITableViewDataSource with Custom Cells Having Subviews

查看:70
本文介绍了使用带有子视图的自定义单元格的UITableViewDataSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在UITableView中使用自定义单元格时,我得到一个奇怪的表重叠:

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

问题


  • 向下滚动&最后两行有两行涂在它们上面!

  • 向上滚动&前两行有两行涂在它们上面!

这是UITableViewDataSource的代码:

Here is the code for the UITableViewDataSource:

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

    let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell!;

    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.textLabel?.text = cellText;    //works fine!
    cell.addSubview(subjectField);        //fails  XX(!

    return cell;
}

这是截图:


  • 注意:'0 9'和'1 A'是油漆!

  • 参考:'0 9'是第0行('0')和第9行('9')

  • Note: '0 9' and '1 A' are the paint overs!
  • Ref: '0 9' is row#0 ('0 ') and row#9 (' 9')

问题


  • 为什么添加子视图导致油漆问题?

  • 什么是正确的方法将自定义视图绘制到单元格中?

上下文


  • 我的实际原始代码在自定义表格视图中使用自定义单元格

  • 此代码示例是原始代码的精简版本。我可以发布任何内容需要帮助解决这个问题!

推荐答案

单元格标识符的问题必须是唯一的

The problem with cell identifier must be unique

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

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

}

这篇关于使用带有子视图的自定义单元格的UITableViewDataSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆