我想知道是否可以根据行索引将单元格内容添加到uitableview中?
例如,如果它是第一个单元格(第0行),我想添加图像和文本。 如果是第二行我想添加一个按钮。 对于所有其他行,我只想添加一行文字。
我尝试使用indexPath.row。它第一次工作,但当我滚动屏幕然后备份我的第一个图像消失时。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
PromoListItem *promo = [promoList objectAtIndex:indexPath.row];
static NSString *CellIdentifier = @"Cell";
AsyncImageView *asyncImageView = nil;
UILabel *label = nil;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(indexPath.row==0)
{
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
CGRect frame;
frame.origin.x = 0;
frame.origin.y = 0;
frame.size.width = 100;
frame.size.height = 100;
asyncImageView = [[[AsyncImageView alloc] initWithFrame:frame] autorelease];
asyncImageView.tag = ASYNC_IMAGE_TAG;
[cell.contentView addSubview:asyncImageView];
frame.origin.x = 110;
frame.size.width =100;
label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.tag = LABEL_TAG;
[cell.contentView addSubview:label];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
} else {
asyncImageView = (AsyncImageView *) [cell.contentView viewWithTag:ASYNC_IMAGE_TAG];
label = (UILabel *) [cell.contentView viewWithTag:LABEL_TAG];
}
NSURL *url = [NSURL URLWithString:promo.imgurl];
[asyncImageView loadImageFromURL:url];
label.text = promo.artistname;
}else
{
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
CGRect frame;
frame.origin.x = 0;
frame.origin.y = 0;
frame.size.width = 100;
frame.size.height = 100;
//asyncImageView = [[[AsyncImageView alloc] initWithFrame:frame] autorelease];
// asyncImageView.tag = ASYNC_IMAGE_TAG;
// [cell.contentView addSubview:asyncImageView];
frame.origin.x = 110;
frame.size.width =100;
label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.tag = LABEL_TAG;
[cell.contentView addSubview:label];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
} else {
// asyncImageView = (AsyncImageView *) [cell.contentView viewWithTag:ASYNC_IMAGE_TAG];
label = (UILabel *) [cell.contentView viewWithTag:LABEL_TAG];
}
//NSURL *url = [NSURL URLWithString:promo.imgurl];
//[asyncImageView loadImageFromURL:url];
label.text = promo.artistname;
}
return cell;
}
答案 0 :(得分:0)
您希望这些细胞如何定制?如果您只是添加文本,图像和附件按钮,则可以在行检查之外创建单元格,然后在if语句中添加所需的项目。
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
NSUInteger row = [indexPath row];
switch(row) {
case 0:
cell.textLabel.text = @"row 0";
cell.image
break;
case 1:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
break;
default:
cell.textLabel.text = [NSString stringWithFormat:@"row %d",row];
break;
}
return cell;
但是,当您开始回收单元格时,行会发生变化,您可能希望在实际数据中包含指示单元格行为方式的内容。