app*_*ing 0 uitableview ios swift
我有一个简单的表格视图。我希望第一行和最后一行有一张特殊的图片,显示一行的开头和结尾。但是,我得到了错误的识别信息。表视图认为中间的单元格是末端单元格。我相信IOS正在缓存行并在需要时加载它们,就像当我向上和向下滚动一些箭头改变图片时(加载后它们应该是静态的)。另外, 文档在这里说 知道如何解决此问题并识别最后一行和第一行,而不管缓存如何?(顺便说一句,位置是用于加载单元格的箭头,数组的第一个和最后一个位置是特殊图片应该在的位置)。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var stop = locations[indexPath.row]
var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! CustomRouteViewCell
cell.locationTitle.text = "\(stop)"
cell.downTime.text = BusRoute.getRecentDepartures(stop, direction: direction, name: name)
println(indexPath.row)
cell.indexRow = indexPath.row
if(cell.indexRow == 0)
{
cell.downImage.image = UIImage(named: "beginningRoute.png")
}
else if(cell.indexRow == locations.count - 1)
{
cell.downImage.image = UIImage(named: "endRoute.png")
}
return cell
}
Run Code Online (Sandbox Code Playgroud)
问题可能不是您错误地识别了第一个和最后一个单元格(只要您的 中只有一个部分,看起来就很好UITableView)。问题可能是重复使用单元格:您没有将单元格设置downImage.image为nil,并且该单元格以前可能是第一个或最后一个单元格。
请尝试以下操作:
switch indexPath.row {
case 0:
cell.downImage.image = UIImage(named: "beginningRoute.png")
case self.tableView(tableView, numberOfRowsInSection: 0) - 1:
cell.downImage.image = UIImage(named: "endRoute.png")
default:
cell.downImage.image = nil
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2276 次 |
| 最近记录: |