在xib文件的每一行中使用uitableview自定义多个单元格
问题描述:
朋友, 我想在xib文件的表格视图中添加多个自定义单元格。 详细我想为表中的每一行添加不同的单元格我试着下面的代码,但似乎不工作我只能自定义一个单元格中的所有行在tableview代码是如下我已经尝试过两个代码 code1是下面在xib文件的每一行中使用uitableview自定义多个单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier;
[email protected]"tablecell";
tablecell *cell = (tablecell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell==nil)
{
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"tablecell" owner:self options:nil];
cell=[nib objectAtIndex:0];
}
[email protected]"gopi";
return cell;
if (indexPath.row==1)
{
NSString *identifier1;
[email protected]"gopicell";
gopicell *cell1=(gopicell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell1==nil)
{
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"gopicell" owner:self options:nil];
cell1=[nib objectAtIndex:0];
}
[email protected]"sabari";
return cell1;
}
}
,而只会显示一个自定义单元格,所以我想这码码2
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier;
if (indexPath.row==0)
{
[email protected]"tablecell";
tablecell *cell = (tablecell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell==nil)
{
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"tablecell" owner:self options:nil];
cell=[nib objectAtIndex:0];
}
[email protected]"gopi";
return cell;
}
else if(indexPath.row==1)
{
[email protected]"gopicell";
gopicell *cell1=(gopicell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell1==nil)
{
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"gopicell" owner:self options:nil];
cell1=[nib objectAtIndex:0];
}
[email protected]"sabari";
return cell1;
}
return nil;
}
我知道这种说法是错误的,但我不知道该怎么回到这里,如果我已经是它可以自定义每行有多个单元格
答
更新您的代码如下。检查偶数和奇数单元格,并将自定义单元格放在您的表格视图中。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier;
if (indexPath.row %2 == 0)
{
[email protected]"tablecell";
tablecell *cell = (tablecell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell==nil)
{
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"tablecell" owner:self options:nil];
cell=[nib objectAtIndex:0];
}
[email protected]"gopi";
return cell;
}
else if(indexPath.row %2 == 1)
{
[email protected]"gopicell";
gopicell *cell1=(gopicell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell1==nil)
{
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"gopicell" owner:self options:nil];
cell1=[nib objectAtIndex:0];
}
[email protected]"sabari";
return cell1;
}
return nil;
}
+0
哇! cooool!拉利特!hatsoff!学习了一个新的话题。那%2是什么意思!? – gopinath
+1
x%2表示x/2的余数是1或0 –
检查此链接 http://stackoverflow.com/questions/5575125/adding-multiple-custom-cells-in-uitableview[enter此处链接说明] [1] 希望它有助于。 [1]:http://stackoverflow.com/questions/5575125/adding-multiple-custom-cells-in-uitableview –
可以附加的图像。你到底想要什么? –