2015-08-13 131 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; 
    } 
} 
相關問題