Configure static cells in uitableview programmatically

I programmatically create a tableview in c lens. How can I make cells static programmatically?

thank

+5
source share
5 answers

Using a separate cell identifier for each of them, you will get it. You can use something like this:

- (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;
}
+9
source

Creating cells in a static programmatic way does not really make sense. Static cells are mainly intended for Interface Builder only and require that the entire TableView be static. They allow you to drag and drop UILables, UITextFields, UIImageViews, etc. Directly to the cells and show how it looks in Xcode when the application starts.

"" , , , , .

UITableViewController .xib , "" . , , , , , .

+12

, , NSIndexPath, Static Cell 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;
}
+3

, , - , , , , , 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) TableviewController xib 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) overload 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);
    }
}
+1

, API . , // , .

MEDeclarativeTable . UITableView. API, .

0

All Articles