以编程方式设置UITableView中的静态单元格

7
我正在以Objective-C编程语言程序化创建一个tableview。请问如何以编程方式将单元格设置为静态的?
谢谢!
5个回答

16
通过编程使单元格静态化并不是非常有意义。 静态单元格基本上只适用于Interface Builder,并需要整个TableView静态化。 它们允许您将UILables、UITextFields、UIImageViews等拖到单元格中,并在运行应用程序时像Xcode中一样显示。
但是,通过不使用外部数据源并硬编码所有内容,您的单元格可以通过编程方式实现“静态”状态,这通常会比较混乱,总体来说不是一个好主意。
如果您想要“静态”单元格,我建议创建一个包含.xib文件的新UITableViewController,并从那里进行自定义。 否则,请硬编码所有值,基本上是相同的事情,但如果可以避免,则可能是不良设计。

9
如果你想避免使用 InterfaceBuilder,通过编程方式创建任意数量的静态 tableview 单元格是完全可以接受的。有很多合理的原因想要最小化使用 InterfaceBuilder。 - MH175

9
通过为每个单元格使用独特的单元格标识符,您将获得它。您可以使用类似于以下内容的东西:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = [NSString stringWithFormat:@"s%i-r%i", indexPath.section, indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
        //you can customize your cell here because it will be used just for one row.
    }

    return cell;
}

3

你也可以采用传统的方式,根据 NSIndexPath 的需求直接创建单元格。这种方式适用于静态单元格TVC和常规表视图(不要忘记在其数据源方法中返回正确的部分和行数):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch indexPath.row {
        case 0:
            // First cell, setup the way you want

        case 1:
            // Second cell, setup the way you want
    }

    // return the customized cell
    return cell;
}

1
如果您想为设置屏幕等创建单元格结构,并且您可能只需要修改一些单元格内容而不是它们的数量或部分结构,则可以像这样重载UITableViewController子类的方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *aCell = [super tableView:tableView cellForRowAtIndexPath:indexPath];

    // Configure the cell...
    if ([aCell.reuseIdentifier isEqualToString:@"someIdentifier"]){
        //some configuration block
    }

    else if ([aCell.reuseIdentifier isEqualToString:@"someOtherIdentifier"]) {
        //other configuration block
    }
    return aCell;
}

但是,您可以通过编写更多的代码以更好的方式完成;
1)在您的.m文件开头添加typedef:
typedef void(^IDPCellConfigurationBlock)(UITableViewCell *aCell);

2) 在您的TableviewController子类扩展中添加cellConfigurations属性:

@interface IPDSettingsTableViewController ()

@property (nonatomic, strong) NSDictionary *cellConfigurations;
@property (nonatomic) id dataModel;

@end

3) 在Storyboard或XIB中修改TableviewController子类的静态单元格,并为要在程序中修改的每个单元格添加唯一的cellReuseIdentifier。

4) 在您的viewDidLoad方法中设置cellsConfiguration块:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self SetupCellsConfigurationBlocks];
}

- (void)SetupCellsConfigurationBlocks
{
    //Store configurations code for each cell reuse identifier
    NSMutableDictionary *cellsConfigurationBlocks = [NSMutableDictionary new];        


    //store cells configurations for a different cells identifiers
    cellsConfigurationBlocks[@"someCellIdentifier"] = ^(UITableViewCell *aCell){
        aCell.backgroundColor = [UIColor orangeColor];
    };

    cellsConfigurationBlocks[@"otherCellIdentifier"] = ^(UITableViewCell *aCell){
        aCell.imageView.image = [UIImage imageNamed:@"some image name"];
    };

    //use waek reference to self to avoid memory leaks
    __weak typeof (self) weakSelf = self;
    cellsConfigurationBlocks[@"nextCellIdentifier"] = ^(UITableViewCell *aCell){
        //You can even use your data model to configure cell
        aCell.textLabel.textColor = [[weakSelf.dataModel someProperty] isEqual:@YES] ? [UIColor purpleColor] : [UIColor yellowColor];
        aCell.textLabel.text      = [weakSelf.dataModel someOtherProperty];
    };
    weakSelf.cellConfigurations = [cellsConfigurationBlocks copy];
}

5) 重载tableView:cellForRowAtIndexPath方法,像这样:

#pragma mark - Table view data source

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *aCell = [super tableView:tableView cellForRowAtIndexPath:indexPath];

    // configure cell
    [self configureCell:aCell withConfigurationBlock:self.cellConfigurations[aCell.reuseIdentifier]];
    return aCell;
}

- (void)configureCell:(UITableViewCell *)aCell withConfigurationBlock:(IDPCellConfigurationBlock)configureCellBlock
{
    if (configureCellBlock){
        configureCellBlock(aCell);
    }
}

0

想要构建一个简单的表格作为菜单或表单是很常见的,但使用内置的API和数据源和委托回调并不容易编写或维护。您可能需要动态添加/删除/更新一些单元格,因此仅使用Storyboard无法解决问题。

我编写了MEDeclarativeTable来以编程方式构建小型表格。它为UITableView提供了数据源和委托。我们最终得到了一个API,其中我们提供部分和行的实例,而不是实现数据源和委托方法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接