Ios UITableView的单元格数
我正在尝试从0-X的数字单元格获取uitableview。目前我正在使用一个标签来做这件事,我只希望标签文本是0,1,2,3,4,…XIos UITableView的单元格数,ios,xcode,uitableview,swift,Ios,Xcode,Uitableview,Swift,我正在尝试从0-X的数字单元格获取uitableview。目前我正在使用一个标签来做这件事,我只希望标签文本是0,1,2,3,4,…X //label for the cell let cellFrame: CGRect = CGRectMake(0, 0, 90, 32) var cellLabel = UILabel(frame: cellFrame) cellLabel.textAlignment = .Center cellLabel.font = UIF
//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
}
我认为问题在于使用indexPath.row获取行索引。在返回单元格之前,只需在函数cellforrowatinedexpath()中创建一个带有添加到单元格的索引的局部变量。然后只需在调用reloadData()之前重置cellIndex=0。我相信这会管用的 i、 e 试试这个:
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
}
比较变量时,必须使用nil
同样在您的情况下,如果单元格为nil,您应该创建一个。因为当你有它时,你试图给一个不存在的单元格添加一个标签。我认为存在与问题相关的可重用单元格。请尝试下面的代码。愿它能帮助你
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
}
请参见,加载表格时,屏幕可见区域的单元格为零。。。这里是4个单元格,所以您的单元格为零,所有4个单元格都满足条件,您将得到4个单元格,其中包含0,1,2,3个文本。但现在,当你向下滚动时,你的单元格将被重新使用,它不会进入条件,因此,它将显示你在上述单元格中添加的旧值,因此将得到错误的值。。您可以使用一种简单的方法:
当单元格为零并在if条件外访问标签时,为标签指定标记(例如1234),使用该标记获取标签并在if条件外为其设置文本。。所以,它每次都会被调用,你会得到你想要的0,1,2,3,…X 将CellForRowatineXpath替换为此
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
}
希望这对您有所帮助。:) 您可能正在重新加载单元格数据?您应该在初始化单元格的块中运行cellLabel.text的代码。我猜只有在cell==null的情况下才会这样做。如果重复使用cells,那么您可能会添加第二个标签…请向我们展示整个cellForRow函数的外观。更改dequeReusableCell。。。初始化。不要重复使用手机,同样的事情也发生了。我有0,1,2,3,4,效果很好,但在那之后是5,上面是0。这不是一个好建议。使用indexPath.row正确获取单元格的索引。
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
}