开发者

UITableView with different cells "behavior"

I'm developing a location-aware application as a project for the university and one of the feature to develop consists of showing the first five points of interest closest to the user. I'm using a table view whose cells style is UITableViewCellStyleSubtitle. I'm having some problems because beyond the title and subtitle of the cell i'm using an image (arrow) to show the direction which the user should follow to reach the POI. This image should rotate as soon as the heading/position of the device change. The problem开发者_JAVA百科 is that only the last row of the table view works and the main problem is that I don't figure out how to use five different cells since each POI can have a different position rather the position of the device thereby each cell's image should rotate independently. I report an image to give a better understanding of my problem.

UITableView with different cells "behavior"

Here follows the code I used for the tableview cells:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *tableIdentifier = @"tableIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:tableIdentifier] autorelease];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.detailTextLabel.backgroundColor = [UIColor clearColor];

    NSUInteger row = [indexPath row];


    cell.detailTextLabel.text = [NSString stringWithFormat:@"Distance: %.3f Km",[...];

    UIImage *cellImage = [UIImage imageNamed:@"arrow.png"];
    cell.imageView.image = cellImage;
    self.cellImageView = cell.imageView;

    return cell;
}

Any ideas of how to use 5 different cells in order to provide each arrow with its right angle?!

Thanks


By doing:

self.cellImageView = cell.imageView.

You are giving yourself a reference only to the very last of the cells. Each time a cell is configured, your cellImageView variable gets updated to point to that cell's image view and you lose your reference to the previous cell's image view.

So, one way of solving your problem is to accumulate the cell image views in an array. Just make sure you keep the image view matched up with the appropriate location; cells can get reused as the tableview displays. But the tableview owns those cells and can mess with them however it wants for caching purposes (hence the reuse identifier).

A better way would be to cooperate with the tableview and let it know when things need changing. Forget about your ivar, and just set the correct heading during cell configuration. Need to change the heading for a cell? Tell the tableview to reload that cell. Need to change the headings for all cells? Just call [[self tableView] reloadData].

Only if you run into issues with noticeable UI slowdowns should you worry about doing something more clever.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜