1

我有一个包含多个部分的表格视图。

我想让其中一些可选,而另一些则不可选。此外,可选择的单元格应链接到不同的 ViewController。

有谁知道如何做到这一点?

4

3 回答 3

1

我发现管理每个单元格信息的最佳方法是创建一个单元格信息类,并让它代表数据。在此类上,您可以标记单元格是否可选择、类型、处理程序等。

然后,在将单元格出列后,根据单元格信息支持对象准备显示。


例子:

@interface MyCellInfo

@property (nonatomic, copy) NSString* title;
@property (nonatomic, copy) NSString* detail;
@property (nonatomic, strong) UIImage* image;
@property (nonatomic, getter=isSelectable) BOOL selectable;
@property (nonatomic) SEL handlingSelector;
...

@end

@implementation MyCellInfo @end

这定义了一个示例单元信息类,该类在每个单元上保存一些属性。为了便于使用,我们将在表中注册一个单元类,以作为示例标识符viewDidLoad

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier: @"ExampleIdentifier"];

您的表视图数据源:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return dataSource.count;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dataSource[section] count];
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"ExampleIdentifier"];

    MyCellInfo* cellInfo = dataSource[indexPath.section];

    if(cellInfo.image == nil)
    {
        //Show image
    }
    else
    {
        [cell.textLabel setText:cellInfo.title];
        [cell.detailTextLabel setText:cellInfo.detail];
    }

    return cell;
}

您的表视图委托:

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
    MyCellInfo* cellInfo = dataSource[indexPath.section];

    return cellInfo.height;
}

- (BOOL)tableView:(UITableView*)tableView shouldHighlightRowAtIndexPath:(NSIndexPath*)indexPath
{
    MyCellInfo* cellInfo = dataSource[indexPath.section];

    return cellInfo.selectable;
}

- (NSIndexPath*)tableView:(UITableView*)tableView willSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    MyCellInfo* cellInfo = dataSource[indexPath.section];

    return cellInfo.selectable ? indexPath : nil;
}

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    MyCellInfo* cellInfo = dataSource[indexPath.section];

    [self performSelector:cellInfo.handlingSelector withObject:cellInfo];
}

剩下的就是让您创建数据并用MyCellInfo对象表示每个条目。我使用了一个数组类型的 db 数组,其中顶级数组表示部分,而每个内部数组是部分的单元格,每个单元格都是一个MyCellInfo对象。

于 2013-05-20T09:19:12.883 回答
0

Apple Developer 网站有一个名为“A Closer Look at UITableViewCell”的页面

位于此处:http: //developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/TableViewCells/TableViewCells.html

您应该关注的部分是:

管理选择 http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/ManageSelections/ManageSelections.html#//apple_ref/doc/uid/TP40007451-CH9-SW6

于 2013-05-19T20:26:44.490 回答
0

在我的另一个问题中,有人给出了同样适用于此的答案。

在 中cellForRowAtIndexpath,您可以设置 if 语句来单独访问每个部分中的每个单元格:

if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            //do any cell setup
        }
        if (indexPath.row == 1) {
            //do any cell setup
        }
   }
if (indexPath.section == 1) {
            if (indexPath.row == 0) {
                //do any cell setup
            }
            if (indexPath.row == 1) {
                //do any cell setup
            }
       }

等等。这使您可以用任何东西填充 cel,还可以为每个单独的单元格进行所需的所有设置。

同样的事情适用于(例如)heightForRowAtIndexPath:您只需插入如上所述的 if 语句,您就可以为任何单个单元格设置高度。

通常,如果你需要为一堆单元格设置相同的东西,你会用一个数组来做,并用它填充 tableview,但如果你真的需要为特定单元格设置特定的设置,这是最简单和最好的方法(在我看来)

希望这对大家有帮助!

于 2013-05-20T11:44:16.643 回答