[英]identifying the last and first cell in a UITableView
我有一個簡單的表格視圖。 我希望第一行和最后一行有一張特殊的圖片,顯示一行的開始和結束。 但是,我得到了錯誤的身份證明。 表格視圖認為中間的單元格是末端單元格。 我相信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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.