我以编程方式创建的目标下的实现代码如下。 我怎样才能让细胞静编程?
谢谢
我以编程方式创建的目标下的实现代码如下。 我怎样才能让细胞静编程?
谢谢
通过使用不同的小区标识为每一个你会得到它。 你可以使用这样的事情:
- (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 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与数据源和委托回调不会很容易地编写或维护。 您可能需要动态地添加/删除/更新一些细胞,所以利用自身故事板将无法工作。
我放在一起MEDeclarativeTable以编程方式生成的小桌子。 它提供了数据源和委托UITableView
。 我们结束了一个API,我们提供的部分和行而不是实现数据源和委托方法的实例。