UITableView的使用两种类型的细胞

问题描述:

enter image description hereUITableView的使用两种类型的细胞

UITableView包含,假设,50行。第一行包含一个项目,每个偶数行索引单元格包含两个项目? 因为我们需要为每个备用行使用两个XIB视图?

totalrowcount和cellAtRowatindexpath逻辑的逻辑是什么?

还有其他的选择吗?

+0

您可以制作两个部分。部分0-行1,部分1-49行 – Durgaprasad

+0

尝试定义2个单元格标识符 – Atrash

+0

问题不明确。在这个问题中,你提到你的第一个细胞是不同的,所有其他的细胞是相同的。然而,你对一些答案的评论表明你有交替的单元格;所有的可能性和均衡都是一样的。 – tolgamorf

我想,你的表是这样的:

所有的
+---------------------+ 
|  Data 1  | --> Cell Type 1 
+---------------------+ 
| Data 2 | Data 3 | --> Cell Type 2 
+---------------------+ 
|  Data 4  | 
+---------------------+ 
| Data 5 | Data 6 | 
+---------------------+ 
.   ...   . 

首先,你应该设计两个不同的UITableViewCell坯的细胞类型。就我个人而言,我会使用Storyboard,设计两个具有不同标识符的动态单元格,并在需要时调用它们。例如:

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
           reuseIdentifier:@"Cell Type 1"]; 

让我们来看看tableView的数据源方法。我假设数据存储在一个名为self.tableData的数组中。你可以建立一个逻辑返回的行数如下:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return ceil([self.tableData count]*(2.0f/3.0f)); 
} 

,然后在cellForRowAtIndexPath方法,返回这些细胞。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell; 
    NSUInteger objectIndex = floor(indexPath.row*(3.0f/2.0f)); 

    if (indexPath.row % 2 == 0) { 
     // define cell as Cell Type 1 from nib 
     cell.yourLabel.text = [self.tableData objectAtIndex:objectIndex]; 
    } else { 
     // define cell as Cell Type 2 from nib 
     cell.yourLabel1.text = [self.tableData objectAtIndex:objectIndex]; 
     cell.yourLabel2.text = [self.tableData objectAtIndex:(objectIndex+1)]; 
    } 

    return cell; 
} 

需要注意的是,我假设你tableData阵列包含有类似NSString对象,你必须UILabel在您的细胞秒。以上代码从相应索引的数据源中提取对象并填充标签。

+0

计算数据源数组返回对象的索引时出错。我相信我修复了它,现在它应该可以工作。 – tolgamorf

+0

总行数的计算是准确的,假设我有5行数据,需要以上述格式显示,计算出错了:count *(2.0/3.0)越来越3.33不是四...有什么想法? –

+0

你确定你正在使用'ceil'功能吗?例如一个快速测试:'NSLog(@“%f”,ceil(5 *(2.0f/3.0f)));',输出是'4.000000'。 – tolgamorf

totalrowcount手段50

的cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // make 2 cellidentifiers here and use it for different nib 
    UITableViewCell *cell; 
    if(indexpath.row==0){ 
    //load first nib 
    } 
    else 
    { 
    //load second nib 
    } 
    return cell; 
} 
+0

totalrowcount逻辑怎么样?每个偶数行包含两个项目行。我们如何计算总行数?我们需要按照itemcount定义行.. –

+0

它是50没有改变那里 –

+0

行数会动态改变..不固定。 –

首先使用- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 和这里通过1个部分为:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 1; 
} 

然后 在- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section方法,你会通过总数作为一个da的行tasource到tableview。 例如:如果你要显示50行再经过50行如:

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

    return 50; 

} 

而且在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 你会通过细胞按照需要 例如:

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


     if(indexPath.row %2 ==0) 
    { 
      // First alternaate cell which you need to display at even index 
      return ALTERNATE_NIB_1; 
    } 
    else 
    { 
      // Second alternaate cell which you need to display at odd index 
     return ALTERNATE_NIB_1; 
    } 


return nil; 
} 

希望它可以帮助你。

+0

每个偶数行将显示列表中的两个项目..行数50不是固定的..按照项目列表进行更改..我们需要根据显示来计算行item .. –

+0

@ user972413检查我更新的答案 –

+0

已更新的uitableview结构 –

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     if(indexPath.row % 2 == 0) { 
      //cell type 1 
     } else if(indexPath.row % 2 == 1) { 
      //cell type 2 
     } 
    } 

上面的代码将帮助您分配备用电池模式

如果你有2种类型的细胞,我建议使用2个不同的标识符,当你出列单元格你的电池需要。