Ios 在UITableView中使用多个(不同)单元格
我正在尝试实现一个tableView设置,在这里我一个接一个地显示包含不同内容的多个单元格。我使用的代码是Ios 在UITableView中使用多个(不同)单元格,ios,Ios,我正在尝试实现一个tableView设置,在这里我一个接一个地显示包含不同内容的多个单元格。我使用的代码是 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *simpleTableIdentifier = @"ScribbleCell"; static NSString *simpleTa
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"ScribbleCell";
static NSString *simpleTableIdentifier2 = @"ScribbleCell2";
static NSString *simpleTableIdentifier3 = @"ScribbleCell3";
UITableViewCell *cell = nil;
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; // or you have the previous 'None' style...
self.tableView.separatorColor = [UIColor clearColor];
if (indexPath.row % 3 == 0) {
// this is a content cell
cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
// get the model index
NSInteger indexInModel = indexPath.row / 3;
NSDictionary *scribble = [scribbles objectAtIndex:indexInModel];
cell.textLabel.text = scribbles[@"name"];
}
else if(indexPath.row % 2 == 0) {
// red color
cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier2];
if(cell == nil)
{
cell = [[[NSBundle mainBundle] loadNibNamed:@"ScribbleCell2" owner:self options:nil] objectAtIndex:0];
}
}else{
// green color
cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier3];
if(cell == nil)
{
cell = [[[NSBundle mainBundle] loadNibNamed:@"ScribbleCell3" owner:self options:nil] objectAtIndex:0];
}
}
return cell;
}
理想的顺序应该是,名字然后是红色,然后是绿色,但事实似乎并非如此。顺序似乎是随机的,在我向下滚动时会发生变化。您应该试试这个
if (indexPath.row % 3 == 0) {
// first cell code
}
else if (indexPath.row % 3 == 1) {
// second cell code
}
else {
// third cell code
}
编辑5个不同的单元格:
if (indexPath.row % 5 == 0) {
// first cell code
}
else if (indexPath.row % 5 == 1) {
// second cell code
}
else if (indexPath.row % 5 == 2) {
// third cell code
}
else if (indexPath.row % 5 == 3) {
// fourth cell code
}
else {
// fifth cell code
}
此外,您还需要确保每条记录有5个单元格,为此,您需要告诉UITableView您有CellsCount=N条记录X每条记录5个单元格。请参见以下代码段
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [myRecords count] * 5;
}
:)是的,马哈茂德这正是我最终实现的,马哈茂德,这似乎不起作用。它正在跳过我的数据。应该有90个帖子,它的帖子只有23个