UITableView有两种类型的单元格

时间:2013-05-29 12:19:53

标签: iphone ios uitableview

enter image description here

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

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

还有其他选择吗?

5 个答案:

答案 0 :(得分:1)

我想你的桌子看起来像这样:

+---------------------+
|        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个。上面的代码从相应的索引中提取数据源中的对象并填充标签。

答案 1 :(得分:0)

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;
}

答案 2 :(得分: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方法中,您将把总行数作为数据源传递给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;
}

希望它对你有所帮助。

答案 3 :(得分:0)

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

上面的代码将帮助您指定备用单元格模式

答案 4 :(得分:0)

如果您有两种类型的细胞,我建议使用2种不同的标识符,当您将细胞出列时,您将获得细胞需要。

相关问题