回答
通过使用不同的单元格标识符为每一个你会得到它。你可以使用类似这样的东西:
以编程方式制作单元格并没有什么意义。静态单元基本上只用于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:
// First 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 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);
}
}
这是很常见的想要建立一个简单的表作为使用菜单或表单,但使用带有数据源和委托回调的内置API并不便于编写或维护。您可能需要动态地添加/删除/更新某些单元格,因此使用Storyboard本身不起作用。
我放在一起以编程方式建立小表。它提供了UITableView
的数据源和代理。我们最终提供了一个API,用于提供节和行的实例,而不是实现数据源和委托方法。
- 1. 以静态方式在UITableViewCell中以编程方式添加控件UITableView
- 2. 以编程方式在datagridview单元格中设置值
- 3. 如何在GridView中以编程方式设置单元值?
- 4. 以编程方式在UITableView中定制单元格?
- 5. 以编程方式在UITableView单元格中添加边距?
- 6. UITableViewController以编程方式访问静态单元格问题
- 7. ios:以编程方式设置静态UITableViewCell(不使用tableView:heightForRowAtIndexPath :)
- 8. 以编程方式设置ICQ状态
- 9. 以编程方式设置状态android
- 10. 以编程方式设置ImageButton状态
- 11. UITableView编辑静态单元格
- 12. 以编程方式添加UITableView - 如何设置单元的重用标识符?
- 13. 以编程方式从UITableView中删除单元格
- 14. UITableView以编程方式滚动到不存在的单元格
- 15. 以静态UITableView编程添加行
- 16. 在Android中以编程方式设置简单数据格式
- 17. 确定UITableView是否以编程方式具有静态单元或动态原型?
- 18. 以编程方式设置iPhone设置
- 19. 无法以编程方式设置单元容器BG
- 20. 如何以编程方式设置DataGridView单元格的值?
- 21. 如何以编程方式设置单元格颜色epplus?
- 22. 如何以编程方式在单个线性布局中设置静态和动态文本
- 23. 如何以编程方式更改静态单元格中的背景图片?
- 24. 以编程方式在Magento中设置用户状态
- 25. 如何以编程方式“点击”UITableView单元格?
- 26. 以编程方式创建的单元格的响应性UITableView
- 27. 以编程方式突出显示UITableView单元格
- 28. 以编程方式向UITableView添加单元格
- 29. 以编程方式向UITableView添加单元格和行
- 30. 如何以编程方式设置UITableView的高度