【发布时间】:2012-06-26 21:49:21
【问题描述】:
我正在以编程方式在目标 c 中创建一个表视图。如何以编程方式使单元格变为静态?
谢谢
【问题讨论】:
标签: objective-c uitableview static cells
我正在以编程方式在目标 c 中创建一个表视图。如何以编程方式使单元格变为静态?
谢谢
【问题讨论】:
标签: objective-c uitableview static cells
通过为每个单元格使用不同的单元格标识符,您将获得它。你可以使用这样的东西:
- (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;
}
【讨论】:
以编程方式将单元格设为静态并没有什么意义。静态单元格基本上只适用于 Interface Builder,并且要求整个 TableView 是静态的。它们允许您将 UILables、UITextFields、UIImageViews 等直接拖到单元格中,并在应用程序运行时显示它在 Xcode 中的外观。
但是,通过不使用外部数据源并对所有内容进行硬编码,您的单元格可以以编程方式“静态”,这通常会有点混乱,而且通常是一个糟糕的主意。
如果您想要“静态”单元格,我建议使用 .xib 制作一个新的 UITableViewController 并从那里对其进行自定义。否则,只需硬编码你所有的值,它基本上是一样的,但如果可以避免的话,可能是糟糕的设计。
【讨论】:
你也可以用老式的方式来创建单元格,这取决于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;
}
【讨论】:
如果您想为设置屏幕或类似的东西创建单元格结构,您可能只需要修改一些单元格内容而不是它们的数量或部分结构,您可以重载 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 扩展:
@interface IPDSettingsTableViewController ()
@property (nonatomic, strong) NSDictionary *cellConfigurations;
@property (nonatomic) id dataModel;
@end
3) 在 storyboard 或 xib 中修改 TableviewController 子类的静态单元格 并为您要以编程方式修改的每个单元格添加唯一的 cellReuseIdentifier
4) 在你的 viewDidLoad 方法中设置单元格配置块:
- (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);
}
}
【讨论】:
想要构建一个简单的表格来用作菜单或表单是很常见的,但是使用带有数据源和委托回调的内置 API 并不容易编写或维护。您可能需要动态添加/删除/更新某些单元格,因此单独使用 Storyboard 是行不通的。
我将MEDeclarativeTable 放在一起以编程方式构建小表。它为UITableView 提供数据源和委托。我们最终得到一个 API,我们在其中提供部分和行的实例,而不是实现数据源和委托方法。
【讨论】: