Multiple Custom Rows UITableView?

梦想与她 提交于 2019-12-18 13:47:07

问题


I've been searching a lot but didn't find anything useful related to multiple custom rows, I need to create a settings tableView for my app,in which I need to load the rows from xib files,like:

ROW 1 =>> XIB 1.
ROW 2 =>> XIB 2.
ROW 3 =>> XIB 3.
ROW 4 =>> XIB 4.

My present code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell=nil;
    //We use CellType1 xib for certain rows
    if(indexPath.row==0){
        static NSString *CellIdentifier = @"ACell";
        cell =(ACell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(cell==nil){
            NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"ACell" owner:self options:nil];
            cell = (ACell *)[nib objectAtIndex:0];
        }
        //Custom cell with whatever
        //[cell.customLabelA setText:@"myText"]
    }
    //We use CellType2 xib for other rows
    else{
        static NSString *CellIdentifier = @"BCell";
        cell =(BCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(cell==nil){
            NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"BCell" owner:self options:nil];
            cell = (BCell *)[nib objectAtIndex:0];
        }
        //Custom cell with whatever
        //[cell.customLabelB setText:@"myText"]
    }

    return cell;
}

回答1:


First you create some custom UITableViewCell classes (.h and .m), as many as you have xib files:
So you could have CellType1 and CellType2 for example.
CellType1.h would look something like

#import <UIKit/UIKit.h>
@interface CellType1 : UITableViewCell

@property(nonatomic,strong) IBOutlet UILabel *customLabel;

@end

Then you create the xib files, you can use default view type, but then, just remove the view that is automatically created, replace that by a UITableViewCell, and change the class to CellType1. Do the same for CellType2.

Then in your tableViewController, write cellForRow like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell=nil;
//We use CellType1 xib for certain rows
if(indexPath.row==<whatever you want>){
     static NSString *CellIdentifier = @"CellType1";
     cell =(CellType1*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     if(cell==nil){
        NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"CellType1" owner:self options:nil];
        cell = (CellType1 *)[nib objectAtIndex:0];
      }
      //Custom cell with whatever
      [cell.customLabel setText:@"myText"]
}
//We use CellType2 xib for other rows
else{
    static NSString *CellIdentifier = @"CellType2";
    cell =(CellType2*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     if(cell==nil){
        NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"CellType2" owner:self options:nil];
        cell = (CellType2 *)[nib objectAtIndex:0];
      }
      //Custom cell with whatever
      [cell.customLabel setText:@"myText"]
}

return cell;
}



回答2:


If you're not already familiar with loading a custom cell from a xib, check out the documentation here. To extend this to multiple custom xibs, you can create each table cell in a separate xib, give it a unique cell identifier, set the table view controller as the file's owner, and connect each cell to the custom cell outlet you define (in the docs, they use tvCell as this outlet). Then in your -tableView:cellForRowAtIndexPath: method, you can load the correct xib (or dequeue the correct reuseable cell) by checking which row you're providing a cell for. For example:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier1 = @"MyIdentifier1";
    static NSString *MyIdentifier2 = @"MyIdentifier2";
    static NSString *MyIdentifier3 = @"MyIdentifier3";
    static NSString *MyIdentifier4 = @"MyIdentifier4";

    NSUInteger row = indexPath.row

    UITableViewCell *cell = nil;

    if (row == 0) {
        cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifer1];
        if (nil == cell) {
            [[NSBundle mainBundle] loadNibNamed:@"MyTableCell1" owner:self options:nil];
            cell = self.tvCell;
            self.tvCell = nil;
        }
    }
    else if (row == 1) {
        cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifer2];
        if (nil == cell) {
            [[NSBundle mainBundle] loadNibNamed:@"MyTableCell2" owner:self options:nil];
            cell = self.tvCell;
            self.tvCell = nil;
        }
    }
    else if (row == 2) {
        cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifer3];
        if (nil == cell) {
            [[NSBundle mainBundle] loadNibNamed:@"MyTableCell3" owner:self options:nil];
            cell = self.tvCell;
            self.tvCell = nil;
        }
    }
    else if (row == 4) {
        cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifer4];
        if (nil == cell) {
            [[NSBundle mainBundle] loadNibNamed:@"MyTableCell4" owner:self options:nil];
            cell = self.tvCell;
            self.tvCell = nil;
        }
    }
    // etc.

    // Do any other custom set up for your cell

    return cell;

}


来源:https://stackoverflow.com/questions/10464712/multiple-custom-rows-uitableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!