2012-06-25 88 views

回答

9

通過使用不同的單元格標識符爲每一個你會得到它。你可以使用類似這樣的東西:

​​
12

以編程方式製作單元格並沒有什麼意義。靜態單元基本上只用於Interface Builder,並且要求整個TableView是靜態的。它們允許您將UILables,UITextFields,UIImageViews等拖入單元格,並在應用程序運行時顯示它在Xcode中的外觀。

但是,通過不使用外部數據源並對所有內容進行硬編碼,您的單元格可以通過編程方式爲「靜態」,這通常會是一種混亂,通常是一個糟糕的主意。

我建議用.xib製作一個新的UITableViewController,如果你想要「靜態」單元,可以從那裏定製它。否則,只需硬編碼所有的值,它基本上是一樣的,但如果可以避免的話,可能是糟糕的設計。

3

你也可以按照你想要的方式創建單元格,具體取決於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; 
} 
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 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); 
    } 
} 
0

這是很常見的想要建立一個簡單的表作爲使用菜單或表單,但使用帶有數據源和委託回調的內置API並不便於編寫或維護。您可能需要動態地添加/刪除/更新某些單元格,因此使用Storyboard本身不起作用。

我放在一起​​以編程方式建立小表。它提供了UITableView的數據源和代理。我們最終提供了一個API,用於提供節和行的實例,而不是實現數據源和委託方法。

相關問題