2015-08-13 130 views
0

我有这个页面,每个单元格都不一样,所以我这样做就像下面的代码,但不知何故,我相信它可以优化,任何想法,我应该使用自定义单元格,但如果是的,那么我有创建12个定制单元,因为每个单元都有完全不同的布局Uitableview不同的单元格

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
// if (cell == nil){ 

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
// } 

    if (indexPath.row==0) 
{ 

UILabel *lbl_label = [[UILabel alloc] initWithFrame:CGRectMake(15, 7, 200, 25)]; 
lbl_label.text = @"PRICE"; 
[lbl_label setFont:NORMAL_LABEL(16)]; 
[lbl_label setTextColor:UIColorFromRGB(0x565A5C,1.0)]; 

    UILabel *lbl_price = [[UILabel alloc] initWithFrame:CGRectMake(100, 7, 200, 25)]; 
    lbl_price.text = @"$ 50"; 
    [lbl_price setFont:NORMAL_LABEL(16)]; 
    [lbl_price setTextColor:[UIColor grayColor]]; 
    [lbl_price setTextColor:UIColorFromRGB(0x82898D,1.0)]; 
    lbl_price.textAlignment = NSTextAlignmentRight; 

    [cell addSubview:lbl_price]; 
[cell addSubview:lbl_label]; 

} 
else if (indexPath.row==1) 
{ 

    UILabel *lbl_label = [[UILabel alloc] initWithFrame:CGRectMake(15, 7, 200, 25)]; 
    lbl_label.text = @"CUISINE"; 
    [lbl_label setFont:NORMAL_LABEL(16)]; 
    [lbl_label setTextColor:UIColorFromRGB(0x565A5C,1.0)]; 


     UILabel *lbl_price = [[UILabel alloc] initWithFrame:CGRectMake(100, 7, 200, 25)]; 
     lbl_price.text = @"Sushi, Japanese, Arabian"; 
     [lbl_price setFont:NORMAL_LABEL(16)]; 
     [lbl_price setTextColor:[UIColor grayColor]]; 
     [lbl_price setTextColor:UIColorFromRGB(0x82898D,1.0)]; 
     lbl_price.textAlignment = NSTextAlignmentRight; 

     [cell addSubview:lbl_price]; 
    [cell addSubview:lbl_label]; 

} 
else if (indexPath.row==2) 
{ 

    UILabel *lbl_label = [[UILabel alloc] initWithFrame:CGRectMake(15, 7, 200, 25)]; 
    lbl_label.text = @"GOOD FOR"; 
    [lbl_label setFont:NORMAL_LABEL(16)]; 
    [lbl_label setTextColor:UIColorFromRGB(0x565A5C,1.0)]; 

     UILabel *lbl_price = [[UILabel alloc] initWithFrame:CGRectMake(100,7, 200, 25)]; 
     lbl_price.text = @"Alcohol, Brunch"; 
     [lbl_price setFont:NORMAL_LABEL(16)]; 
     [lbl_price setTextColor:[UIColor grayColor]]; 
     [lbl_price setTextColor:UIColorFromRGB(0x82898D,1.0)]; 
     lbl_price.textAlignment = NSTextAlignmentRight; 

     [cell addSubview:lbl_price]; 
    [cell addSubview:lbl_label]; 

cell.selectionStyle = UITableViewCellSelectionStyleNone; 

return cell; 

}

回答

0

如果你喜欢使用自定义单元格,那么你可以按照下面给出使用。

首先,要注册所有12个细胞的NIB: 并注册后,你必须处理它在细胞

- (void)viewDidLoad{ 
    [super viewDidLoad]; 

    static NSString *CellIdentifier1 = @"ContentCell"; 
    static NSString *CellIdentifier2 = @"SpaceCell"; 

    UINib *nib = [UINib nibWithNibName:@"CellViewNIBName" bundle:nil]; 
    [self.tableView registerNib:nib forCellReuseIdentifier:CellIdentifier1]; 

    nib = [UINib nibWithNibName:@"CellSpaceNIBName" bundle:nil]; 
    [self.tableView registerNib:nib forCellReuseIdentifier:CellIdentifier2]; 

    self.contentView.hidden = YES; 
    [self loadData]; 
} 

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath 
{ 
    static NSString *CellIdentifier1 = @"ContentCell"; 
    static NSString *CellIdentifier2 = @"SpaceCell"; 

    // Space Cell 
    if (indexPath.row % 2 == 1) { 
     CellSpace *cell = (CellSpace *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; 
     return cell; 
    } 

    // Content cell 
    else { 
     CellView *cell = (CellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; 
     // Configure cell 
     return cell; 
    } 
} 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Space cell's height 
    if (indexPath.row % 2 == 1) { 
     return 20.0f; 
    } 

    // Content cell's height 
    else { 
     return 80.0f; 
    } 
} 
相关问题