我正在尝试实现一个tableView设置,其中我一个接一个地显示其中包含不同内容的多个单元格。我正在使用的代码是
- (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;
}
理想的顺序应该是,然后名称为红色然后是绿色,但似乎并非如此。订单似乎是随机的,随着我向下滚动而改变。
答案 0 :(得分:1)
你应该试试这个
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;
}