2015-04-23 68 views
0

我想从0-X数字单元格为一个uitableview。目前我正在使用一个标签来做到这一点,我只想标签文本为0,1,2,3,4,... X。为UITableView数字单元格

//label for the cell 
    let cellFrame: CGRect = CGRectMake(0, 0, 90, 32) 
    var cellLabel = UILabel(frame: cellFrame) 
    cellLabel.textAlignment = .Center 
    cellLabel.font = UIFont.MDFont.regularFourteen 
    cellLabel.text = ("\(indexPath.row)") 

我已经试过这种方法,它只是做0,1,2,3,4但是当我把它的滚动的其它号码的顶部打印0,1,2,3,4为好。

这是功能

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell 

    if cell == null { 
     //label for the cell 
     let cellFrame: CGRect = CGRectMake(0, 0, 90, 32) 
     var cellLabel = UILabel(frame: cellFrame) 
     cellLabel.textAlignment = .Center 
     cellLabel.font = UIFont.MDFont.regularFourteen 
     cellLabel.text = ("\(indexPath.row)") 
     cell.contentView.addSubview(cellLabel) 
    } 


    //print("Table data is \(tableData[5])") 
    return cell 
} 
+0

您可能会重新加载单元格数据? –

+1

您应该在初始化单元的块中运行cellLabel.text的代码。我猜你只是在cell == null时才这样做。 – Krzysztof

+1

单元格被重复使用,所以当发生这种情况时,您可能会添加第二个标签... – Volker

回答

-1

我认为这个问题是使用indexPath.row让你的行索引整个代码。在返回单元格之前,只需使用在函数cellForRowAtIndexPath()中添加到单元格中的索引创建一个局部变量。然后在调用reloadData()之前重置cellIndex = 0。我认为,将工作

var cellIndex = 0 

func ..cellForRowAtIndexPath ...() 
{ 
//set the index to the cell label. 
cell.label = cellIndex++ 
return cell 
} 
+0

发生同样的事情。我有0,1,2,3,4这很好,但在此之后,它是0,最高为0。 –

+1

不,这不是一个好建议。使用indexPath.row获取单元格的索引权。 – pteofil

0

试试这个:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell 

    if let cellLabel = cell.contentView.viewWithTag(10) as? UILabel { 
     cellLabel.text = ("\(indexPath.row)") 
    } 
    else { 
     let cellLabel = UILabel(frame: CGRectMake(0, 0, 90, 32)) 
     cellLabel.tag = 10 
     cellLabel.textAlignment = .Center 
     cellLabel.font = UIFont.MDFont.regularFourteen 
     cellLabel.text = ("\(indexPath.row)") 
     cell.contentView.addSubview(cellLabel) 
    } 

    //print("Table data is \(tableData[5])") 
    return cell 
} 

当您比较的变量,你必须使用为零。

同样在你的情况下,如果单元格为零,你应该创建一个。因为你有它,你尝试添加一个标签到一个不存在的单元格。

0

我认为有问题相关的可重用细胞。请尝试下面的代码。它可以帮助你。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell 

    if cell == null { 
     //label for the cell 
     let cellFrame: CGRect = CGRectMake(0, 0, 90, 32) 
     var cellLabel = UILabel(frame: cellFrame) 
     cellLabel.textAlignment = .Center 
     cellLabel.font = UIFont.MDFont.regularFourteen 
     cellLabel.text = ("\(indexPath.row)") 
     cell.contentView.addSubview(cellLabel) 
    } 
    else { 
     for obj : AnyObject in cell.contentView.subviews { 
     if let label = obj as? UILabel { 
      label.text = ("\(indexPath.row)") 
     } 
    } 
    } 


    //print("Table data is \(tableData[5])") 
    return cell 
} 
0

看到的,当加载你的表,你的细胞是零用于屏幕的可视区域......在这里,它的4个单元,让您的电池是零条件将得到满足所有4个细胞,你会获得具有0,1,2,3文本的4个单元格。但是现在当你向下滚动时,你的单元格将被重用,并且它不会进入条件内部,它会显示你在上面的单元格中添加的旧值,因此会得到错误的值。你可以使用简单的方法: 当您的单元格为零并在if条件之外访问它时,为您的标签赋予标签(例如1234),使用该标签获取该标签并在if条件之外设置文本。因此,它会被每次调用,你会得到0,1,2,3,.... X,你想...

0

用这个替换cellForRowAtIndexPath。

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

var cell : UITableViewCell = UITableViewCell(frame: CGRectMake(0, 0, tableView.frame.size.width, 44)) 

let cellLabel = UILabel(frame: CGRectMake(0, 0, 90, 32)) 
cellLabel.tag = 10 
cellLabel.textAlignment = .Center 
cellLabel.text = ("\(indexPath.row)") 
cell.addSubview(cellLabel) 

return cell 
} 

希望这可以帮助你。 :)