如何将图片添加到UITableView单元格中?

6

我临时通过编程的方式向UITableViewCell添加了一些值。但是我需要为每个单元格添加图像。我该怎么做?

这是我的代码。.h文件

 @interface BidalertsViewController : UIViewController       <UITableViewDelegate,UITableViewDataSource> {
    NSArray *listData;
}
@property(nonatomic, retain) NSArray *listData; 
@end

.m file

 @synthesize listData;

- (void)viewDidLoad {
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(5,0,310,28)];
newView.backgroundColor=[UIColor whiteColor];
UITextView *mytext = [[UITextView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 25.0)];
mytext.backgroundColor = [UIColor clearColor];
mytext.textColor = [UIColor blackColor];
mytext.editable = NO;
mytext.font = [UIFont systemFontOfSize:15];
mytext.text = @"Asset Name";
[mytext release];
[newView addSubview:mytext];

[self.view addSubview:newView];
[newView release];


NSArray *array = [[NSArray alloc] initWithObjects:@"iPhone", @"iPod", @"iPad",nil];
self.listData = array;
[array release];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
     [super didReceiveMemoryWarning];
}
 - (void)dealloc {
  [listData dealloc];
  [super dealloc];
}


 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
return [self.listData count];
 }

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil) { cell = [[[UITableViewCell alloc]
                            initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row]; 
cell.textLabel.text = [listData objectAtIndex:row]; 
return cell;

UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)];
imv.image=[UIImage imageNamed:@"user.jpg"];
[cell.contentView addSubview:imv];
[imv release];
 }
@end

这段代码没有将图片添加到单元格中,可能出了什么问题?我该如何添加自定义图片到单元格中呢?

5个回答

12

return cell; 语句移到方法末尾:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
    if (cell == nil) { cell = [[[UITableViewCell alloc]
                                initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
    }
    NSUInteger row = [indexPath row]; 
    cell.textLabel.text = [listData objectAtIndex:row]; 


    UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)];
    imv.image=[UIImage imageNamed:@"user.jpg"];
    [cell.contentView addSubview:imv];
    [imv release];

    return cell;
}

图片已经显示,但是图片在文字前面显示,导致文字无法完全显示。 - Kattu Poochi
3
它能正常工作是因为textLabel的框架与你的UIImageView相交。可能你需要一个自定义单元格。 - beryllium

7
您可以使用单元格的imageView属性添加图片:
cell.imageView.image = [UIImage imageNamed:@"user.jpg"];

在你的- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法中。


是的,我也添加到资源文件中了。 - Kattu Poochi
是的,当然可以。它被显示了,但是图片显示在文字前面,导致文字无法完全显示。 - Kattu Poochi
在这种情况下,您需要使用自定义单元格,子类化UITableViewCell类。您可以按照此教程进行操作:http://ijoshsmith.com/2011/07/16/creating-a-custom-uitableviewcell-in-ios-4/ 或搜索“custom uitableviewcell”。 - Matteo Alessani

0
这是一个在表格单元格中加载 Facebook 图片的示例单元格。
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                       reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    // Leave cells empty if there's no data yet
    if (nodeCount > 0)
    {
        // Set up the cell...
        AppRecord *appRecord = [self.friendsList objectAtIndex:indexPath.row];

        cell.textLabel.text = friend.name;

        // Only load cached images; defer new downloads until scrolling ends
        if (!appRecord.icon)
        {
            // set the url
            appRecord.imageURLString = [NSString stringWithFormat:@"http://graph.facebook.com/%@/picture", friend.recordID];

            // request the download
            if (self.tableView.dragging == NO && self.tableView.decelerating == NO)
            {
                [self startIconDownload:appRecord forIndexPath:indexPath];
            }
            // if a download is deferred or in progress, return a placeholder image
            cell.imageView.image = [UIImage imageNamed:@"Placeholder.png"];                
        }
        else
        {
            cell.imageView.image = appRecord.icon;
        }

    }

    return cell;

0

你加对了,但是你忘记了一件事:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
    if (cell == nil) { cell = [[[UITableViewCell alloc]
                                initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
    }
    NSUInteger row = [indexPath row]; cell.textLabel.text = [listData objectAtIndex:row];

    UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)];
    imv.image=[UIImage imageNamed:@"user.jpg"];
    [cell.contentView addSubview:imv];
    [imv release];

    //you fogot this:
    return cell;
     }

它不会起作用,因为您已经在上面有return cell; ;) - beryllium
哦...在这行的末尾,是的。然后只需替换它。 - SentineL
我刚刚进行了编辑。问题出在这里:你将未完成的单元格返回给了tableView。我的意思是,你先返回了它,然后再添加图像,所以它被忽略了。 - SentineL
哦,很好。谢谢回复。图片已经显示出来了。但是它在文字前面显示,文字被遮挡了。如何分离文字和图片? - Kattu Poochi
尝试使用Matteo Alessani的解决方案(在此问题上得到了赞同的答案)。如果它没有帮助,您将需要添加自己的标签,而不是通常的标签,就像您添加图像一样。 - SentineL

0

// 为图像视图创建一个矩形框

UIImageView *iconImage = [[UIImageView alloc] init];
iconImage.frame = CGRectMake(12,4,53.5,53.5);

// Create a label for cells
UILabel *cellName = [[UILabel alloc] initWithFrame:CGRectMake(75,18,200,20)];
cellName.font = [self fontForBodyTextStyle];

NSString *cellNameStr = [NSString stringWithFormat:@"%@",self.tableCellNames[indexPath.row]];

// Select path row tab actions
switch (indexPath.row) {
    case 0:

        cellName.text = cellNameStr;
        iconImage.image = [UIImage imageNamed:@"condition.png"];
        [cell.contentView addSubview:iconImage];
        [cell.contentView addSubview:cellName];

        break;

    case 1:
       cellName.text = cellNameStr;
        iconImage.image = [UIImage imageNamed:@"videos.png"];
        [cell.contentView addSubview:iconImage];
        [cell.contentView addSubview:cellName];
        break;

    case 2:
        cellName.text = cellNameStr;
        iconImage.image = [UIImage imageNamed:@"provider.png"];
        [cell.contentView addSubview:iconImage];
        [cell.contentView addSubview:cellName];
        break;

    case 3:
        cellName.text = cellNameStr;
        iconImage.image = [UIImage imageNamed:@"info.png"];
        [cell.contentView addSubview:iconImage];
        [cell.contentView addSubview:cellName];
        break;

    default:
        NSAssert(NO, @"Unhandled value in cellForRowAtIndexPath");
        break;
}

感谢 VKJ,


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接