2012-06-25 70 views

回答

9

通过使用不同的单元格标识符为每一个你会得到它。你可以使用类似这样的东西:

​​
12

以编程方式制作单元格并没有什么意义。静态单元基本上只用于Interface Builder,并且要求整个TableView是静态的。它们允许您将UILables,UITextFields,UIImageViews等拖入单元格,并在应用程序运行时显示它在Xcode中的外观。

但是,通过不使用外部数据源并对所有内容进行硬编码,您的单元格可以通过编程方式为“静态”,这通常会是一种混乱,通常是一个糟糕的主意。

我建议用.xib制作一个新的UITableViewController,如果你想要“静态”单元,可以从那里定制它。否则,只需硬编码所有的值,它基本上是一样的,但如果可以避免的话,可能是糟糕的设计。

3

你也可以按照你想要的方式创建单元格,具体取决于NSIndexPath,它适用于静态单元TVC和常规表格视图(不要忘记返回适当数量的节和行在他们的数据源的方法):

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

     case 1: 
      // First 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)添加cellConfigurations属性为您TablviewControllerSubclass extention:

@interface IPDSettingsTableViewController() 

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

@end 

3)修改TableviewController子的你的静态细胞在故事板或xib 中,并为要编程修改的每个单元格添加唯一的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本身不起作用。

我放在一起​​以编程方式建立小表。它提供了UITableView的数据源和代理。我们最终提供了一个API,用于提供节和行的实例,而不是实现数据源和委托方法。

相关问题