简体   繁体   中英

identifying the last and first cell in a UITableView

I have a simple table view. I want the first and last rows to have a special picture showing the beginning and end of a line. However, I am getting the wrong identification. The table view thinks that cells in the middle are the ends cells. I believe that IOS is caching the rows and and loading them when there needed as when I scroll up and down some of the arrows change picture(they should be static once loaded). Also the documentation says here Any idea how to fix this and identify the last and first row regardless of caching? (Btw locations is the arrow used to load the cells and the first and last positions of the array are where the special picture should be).

表格视图图像

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
}

The problem may not be you're misidentifying the first and last cells (that looks fine so long as you've only got one section in your UITableView ). The problem could be reusing cells: you're not setting the cell's downImage.image to nil and the cell may previously have been the first or last cell.

Try the following:

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
}  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM