UITableView的使用两种类型的细胞
UITableView
包含,假设,50行。第一行包含一个项目,每个偶数行索引单元格包含两个项目? 因为我们需要为每个备用行使用两个XIB
视图?
totalrowcount和cellAtRowatindexpath
逻辑的逻辑是什么?
还有其他的选择吗?
我想,你的表是这样的:
所有的+---------------------+
| 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
在您的细胞秒。以上代码从相应索引的数据源中提取对象并填充标签。
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;
}
totalrowcount逻辑怎么样?每个偶数行包含两个项目行。我们如何计算总行数?我们需要按照itemcount定义行.. –
它是50没有改变那里 –
行数会动态改变..不固定。 –
首先使用- (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;
}
希望它可以帮助你。
每个偶数行将显示列表中的两个项目..行数50不是固定的..按照项目列表进行更改..我们需要根据显示来计算行item .. –
@ user972413检查我更新的答案 –
已更新的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个不同的标识符,当你出列单元格你的电池需要。
您可以制作两个部分。部分0-行1,部分1-49行 – Durgaprasad
尝试定义2个单元格标识符 – Atrash
问题不明确。在这个问题中,你提到你的第一个细胞是不同的,所有其他的细胞是相同的。然而,你对一些答案的评论表明你有交替的单元格;所有的可能性和均衡都是一样的。 – tolgamorf