2015-07-10 68 views
1

朋友, 我想在xib文件的表格視圖中添加多個自定義單元格。 詳細我想爲表中的每一行添加不同的單元格我試着下面的代碼,但似乎不工作我只能自定義一個單元格中的所有行在tableview代碼是如下我已經嘗試過兩個代碼 code1是下面在xib文件的每一行中使用uitableview自定義多個單元格

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

{ 
    NSString *identifier; 

    [email protected]"tablecell"; 
    tablecell *cell = (tablecell*)[tableView dequeueReusableCellWithIdentifier:identifier]; 
    if (cell==nil) 
    { 
     NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"tablecell" owner:self options:nil]; 
     cell=[nib objectAtIndex:0]; 
    } 
    [email protected]"gopi"; 
    return cell; 


    if (indexPath.row==1) 
    { 
     NSString *identifier1; 
     [email protected]"gopicell"; 
     gopicell *cell1=(gopicell*)[tableView dequeueReusableCellWithIdentifier:identifier]; 
     if (cell1==nil) 
     { 
      NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"gopicell" owner:self options:nil]; 
      cell1=[nib objectAtIndex:0]; 
     } 

     [email protected]"sabari"; 
     return cell1; 
    } 
} 

,而只會顯示一個自定義單元格,所以我想這碼碼2

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

{ 
    NSString *identifier; 
    if (indexPath.row==0) 
    { 
     [email protected]"tablecell"; 
     tablecell *cell = (tablecell*)[tableView dequeueReusableCellWithIdentifier:identifier]; 
     if (cell==nil) 
     { 
      NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"tablecell" owner:self options:nil]; 
      cell=[nib objectAtIndex:0]; 
     } 
     [email protected]"gopi"; 
     return cell; 
    } 
    else if(indexPath.row==1) 
    { 
     [email protected]"gopicell"; 
     gopicell *cell1=(gopicell*)[tableView dequeueReusableCellWithIdentifier:identifier]; 
     if (cell1==nil) 
     { 
      NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"gopicell" owner:self options:nil]; 
      cell1=[nib objectAtIndex:0]; 
     } 

     [email protected]"sabari"; 
     return cell1; 
    } 
    return nil; 
} 

我知道這種說法是錯誤的,但我不知道該怎麼回到這裏,如果我已經是它可以自定義每行有多個單元格

+0

檢查此鏈接 http://stackoverflow.com/questions/5575125/adding-multiple-custom-cells-in-uitableview[enter此處鏈接說明] [1] 希望它有助於。 [1]:http://stackoverflow.com/questions/5575125/adding-multiple-custom-cells-in-uitableview –

+0

可以附加的圖像。你到底想要什麼? –

回答

1

更新您的代碼如下。檢查偶數和奇數單元格,並將自定義單元格放在您的表格視圖中。

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

{ 
    NSString *identifier; 
    if (indexPath.row %2 == 0) 
    { 
     [email protected]"tablecell"; 
     tablecell *cell = (tablecell*)[tableView dequeueReusableCellWithIdentifier:identifier]; 
     if (cell==nil) 
     { 
      NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"tablecell" owner:self options:nil]; 
      cell=[nib objectAtIndex:0]; 
     } 
     [email protected]"gopi"; 
     return cell; 
    } 
    else if(indexPath.row %2 == 1) 
    { 
     [email protected]"gopicell"; 
     gopicell *cell1=(gopicell*)[tableView dequeueReusableCellWithIdentifier:identifier]; 
     if (cell1==nil) 
     { 
      NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"gopicell" owner:self options:nil]; 
      cell1=[nib objectAtIndex:0]; 
     } 

     [email protected]"sabari"; 
     return cell1; 
    } 
    return nil; 
} 
+0

哇! cooool!拉利特!hatsoff!學習了一個新的話題。那%2是什麼意思!? – gopinath

+1

x%2表示x/2的餘數是1或0 –

相關問題