Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 具有两种单元格类型的UITableView_Iphone_Ios_Uitableview - Fatal编程技术网

Iphone 具有两种单元格类型的UITableView

Iphone 具有两种单元格类型的UITableView,iphone,ios,uitableview,Iphone,Ios,Uitableview,UITableView假设包含50行。第一行包含一项,每偶数行索引单元格包含两项? 为此,我们需要为每个备用行使用两个XIB视图 totalrowcount和CellaroWatIndeXPath逻辑的逻辑是什么 还有其他选择吗?总行数为50 cellForRowAtIndexpath - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

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

totalrowcount和
CellaroWatIndeXPath
逻辑的逻辑是什么


还有其他选择吗?

总行数为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;
}
首先使用
-(NSInteger)tableView:(UITableView*)tableView行数section:(NSInteger)节
并在此处传递1节,如下所示:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
然后 在
-(NSInteger)tableView:(UITableView*)tableView numberofrowsinssection:(NSInteger)section
方法中,您将把总行数作为数据源传递给tableView。 例如:如果必须显示50行,则将50行传递为:

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

    return 50;

}
-(UITableViewCell*)tableView:(UITableView*)tableView CellforRowatineXpath:(NSIndexPath*)indexPath中
您将根据需要传递单元格 例:

希望对你有帮助

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

如果您有两种类型的单元格,上面的代码将帮助您分配备用单元格模式,我建议您使用两种不同的标识符,当您将单元格出列时,您将获得所需的单元格

我想你的桌子是这样的:

+---------------------+
|        Data 1       |  --> Cell Type 1
+---------------------+
|  Data 2  |  Data 3  |  --> Cell Type 2
+---------------------+
|        Data 4       |
+---------------------+
|  Data 5  |  Data 6  |
+---------------------+
.         ...         .
首先,您应该为这些单元格类型设计两个不同的
UITableViewCell
nib。就我个人而言,我会使用故事板,设计两个具有不同标识符的动态单元,并在需要时调用它们。例如:

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));
}
然后在
cellforrowatinexpath
方法中,返回这些单元格

- (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节第1行第1节第49行尝试定义2个单元格标识符问题不清楚。在问题中,您提到您的第一个单元格是不同的,而所有其他单元格都是相同的。然而,你对一些答案的评论表明你有交替的细胞;所有几率和偶数都是相同的。您应该更清楚地说明,并提供有关单元格结构和数据源的一些信息,以便我们对逻辑进行评论。更新的uitableview结构totalrowcount逻辑如何?每个偶数行包含两个项目行。我们如何计算总行数?我们需要根据itemcount定义行。行数将动态更改。不固定。@RajeshPillai为什么不能从数据源传递行数?然后在numberOfrowsInSection方法中保持它的动态每个偶数行将显示列表中的两个项目。行数50不固定。根据项目列表变化。我们需要根据需要计算行数每个显示项..@user972413我已更新我的答案。使用一个部分,然后在给定条件下为备用索引添加单元格,如代码中所述。它将正常工作计算从数据源数组返回对象的索引时出错。我相信我已经解决了,现在应该可以工作了。行总数计算是准确的,假设我有5个数据行,需要以上述格式显示,计算出错:count*(2.0/3.0)得到的是3.33而不是4…有什么想法吗?你确定你在使用
ceil
函数吗?例如,快速测试:
NSLog(@“%f”,ceil(5*(2.0f/3.0f)),输出为
4.000000
- (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;
}