我是以编程方式在目标c中创建一个tableview。如何以编程方式使单元格静态?
由于
答案 0 :(得分:14)
以编程方式使单元格静态并不真正有意义。静态单元基本上仅用于Interface Builder,并且要求整个TableView是静态的。它们允许您将UILables,UITextFields,UIImageViews等直接拖到单元格中,并让它在应用程序运行时显示它在Xcode中的外观。
然而,你的细胞可以是静止的"以编程方式不使用外部数据源并对所有内容进行硬编码,这通常会是一种混乱,通常是一个糟糕的想法。
我建议使用.xib创建一个新的UITableViewController并根据需要自定义它" static"细胞。否则,只需对所有值进行硬编码,它基本上是相同的,但如果可以避免则可能设计不佳。
答案 1 :(得分: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;
}
答案 2 :(得分: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;
}
答案 3 :(得分: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扩展中:
@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);
}
}
答案 4 :(得分:0)
It is pretty common to want to build a simple table to use as a menu or form, but using the built in API with the datasource and delegate callbacks don't make it easy to write or maintain. You may need to dynamically add/remove/update some cells, so using Storyboards by itself won't work.
I put together MEDeclarativeTable to programmatically build small tables. It provides the datasource and delegate for let layer = CALayer()
layer.contentsScale = UIScreen.mainScreen().scale
layer.frame = CGRect(x: 30, y: 30, width: 30, height: 35)
UIGraphicsBeginImageContextWithOptions(layer.bounds.size, false, 0)
let con = UIGraphicsGetCurrentContext()
CGContextSetShadowWithColor(con, CGSizeMake(0,3), 1, UIColor.blackColor().CGColor)
let path = UIBezierPath(roundedRect: CGRectMake(0,0,30,30), cornerRadius: 4)
UIColor.yellowColor().setFill()
path.fill()
let im = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
layer.contents = im.CGImage
view.layer.addSublayer(layer)
. We end up with an API where we provide instances of sections and rows instead of implementing datasource and delegate methods.