[英]using multiple (different) cells in UITableView
我正在尝试实现一个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;
}
理想的顺序应该是名称,然后是红色,然后是绿色,但是事实并非如此。 顺序似乎是随机的,并且随着我向下滚动而变化。
你应该试试这个
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.