我有一个简单的表格视图。我希望第一行和最后一行具有一张特殊的图片,以显示一行的开始和结束。但是,我得到了错误的身份证明。表格视图认为中间的单元格是末端单元格。我相信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
}
问题可能不在于您误认了第一个和最后一个单元格(只要您只包含一个部分,这看起来就很好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
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句