0

I have a tableview with a few items. I want to get a new view when I click on one of the cells. However, I want the view to be different depending on which cell I clicked. For example: I have a tableview of buildings. These buildings can be Airports, factories and houses.

When I click on a cell that shows an airport, I want the next view to be different than when I click on a cell that shows a factory.

How do I do this?

4

1 回答 1

0

通过给每个单元格一个类型可以很容易地解决这个问题。如果您正在实现自定义单元类,请将其添加为枚举类型(提高可读性)

// cell header file

typedef enum {
    CellTypeAirports,
    CellTypeFactories,
    CellTypeHouses
} CellType;

@interface CustomTableCell

// ...
@property(assign) CellType type;    

@end

在实例化(出列)单元格时设置类型,然后在您的tableView:didSelectRowAtIndexPath:方法中检查单元格的类型

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // ...
    if ([cell type] == CellTypeAirports) {
        // push airports vc
    } else if ([cell type] == CellTypeFactories) {
        // factories
    }
    // and so on

}

如果您使用UITableViewCell的是标准,那么您可以利用它们的tag属性来存储类型。

于 2013-10-27T09:43:15.793 回答