2013-02-07 85 views
14

我有一个关于3种不同类型原型单元格的表格视图的简单问题。前两次只发生一次,而第三次发生四次。现在我感到困惑的是如何在我的cellforRowatindexpath中指定哪个单元格原型用于哪一行。所以,我想要的东西就像第0行,使用原型1,第1行,使用原型2,第3,4,5和第6行使用原型3.什么是最好的方法来做到这一点?我是否给每个原型标识符,然后使用dequeueReusableCellWithIdentifier:CellIdentifier? 你能提供一些示例代码吗?TableView与多个原型单元格

编辑:

仍然无法正常工作。这是我目前的代码。 (我只有在一种情况下的switch叙述,因为我只是想测试一下,看看是否是第一行中产生与否的细胞,但目前表视图为空)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    switch(indexPath.row) 
{   
case 0: {static NSString *CellIdentifier = @"ACell"; 
        UITableViewCell *cell = [tableView 
              dequeueReusableCellWithIdentifier:@"ACell"]; 
    if(cell==nil) { 
    cell=[[UITableViewCell alloc] 
      initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"ACell"]; 

         } 
    return cell; 
    break; 
    } 
    } 
} 

Acell是我标识符我创建的细胞原型。我

+0

我不undertand你的问题。 –

+2

“我是否给每个原型标识符,然后使用dequeueReusableCellWithIdentifier:CellIdentifier?”是的你是。你几乎已经回答了你自己的问题。 – rdelmar

+0

但我该如何选择哪一个原型适用于哪一行? –

回答

16

如果您使用三个原型,然后使用三个标识符。因为只有一个标识符会导致问题。你会得到错误的结果。所以这样的代码。

if(indexPath.row==0){ 
// Create first cell 
} 

if(indexPath.row==1){ 
// Create second cell 
} 

else{ 
// Create all others 
} 

您也可以在这里使用开关盒以获得最佳性能。

+0

在创建单元格期间,您可以按照相同的方法,然后使用单个标识符。但在这里,您将在deque期间使用三个标识符。 –

3
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 

    if (cell.tag == 0) 
    { 
    return array1.count; 
    } 
    else(cell.tag == 1) 
    { 
    return array2.count; 
    }  
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *cellIdentifier; 

NSString *membershipType = [membershipTypeArray objectAtIndex:indexPath.row]; 

if ([membershipType isEqualToString:@"silver"]||[membershipType isEqualToString:@"gold"]) 
{ 
    cellIdentifier = @"cell"; 
} 
else if ([membershipType isEqualToString:@"platinum"]) 
{ 
    cellIdentifier = @"premiumCustomCell"; 
    cell.customCellImageView.image = [cellImageArray objectAtIndex:indexPath.row]; 
} 

cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

if (!cell) { 
    cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
} 
cell.selectionStyle = UITableViewCellSelectionStyleNone; 
cell.headingLabel.text = [titleArray objectAtIndex:indexPath.row]; 
} 
+0

如何处理没有。节中的行。如果您有2个原型单元格并且没有要根据阵列数显示的行? –

+0

您只需标记您的自定义原型单元格,并检查每个单元格的数组数以返回'array.count' –

+0

@surajkthomas请参阅上面的我的编辑 –

1

在这里,我写了这样的代码:

#pragma mark == Tableview Datasource 

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 2; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
NSInteger nRows = 0; 
switch (section) { 
    case 0: 
     nRows = shipData.count; 
     break; 
    case 1: 
     nRows = dataArray1.count; 
     break; 
    default: 
     break; 
} 
return nRows; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
NSString *cellIdentifier = @"cellIdentifier1"; 
NSString *cellIdentifier1 = @"cellIdentifier2"; 
SingleShippingDetailsCell *cell; 
switch (indexPath.section) { 
    case 0: 
     cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
     //Load data in this prototype cell 
     break; 
    case 1: 
     cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier1]; 
     //Load data in this prototype cell 
     break; 
    default: 
     break; 
} 
return cell; 
} 
相关问题