Objective c 以编程方式将图像添加到TableView单元格

Objective c 以编程方式将图像添加到TableView单元格,objective-c,iphone,xcode,swift,Objective C,Iphone,Xcode,Swift,我有一个UITableView,它从服务器获取信息并将数据发布到表视图中。每个单元格内都有来自服务器的信息 在本例中,假设我们从服务器获得的信息是数字:1、2、3、4 我想做的是在单元格的左侧添加一个图像(以编程方式,因为涉及if语句等),并在其旁边添加文本(1、2等) 基本上,我希望每个单元格都像这样: _____________________________________ | (IMAGE) (TEXT) | --CELL 0 --------

我有一个
UITableView
,它从服务器获取信息并将数据发布到表视图中。每个单元格内都有来自服务器的信息

在本例中,假设我们从服务器获得的信息是数字:1、2、3、4

我想做的是在单元格的左侧添加一个图像(以编程方式,因为涉及if语句等),并在其旁边添加文本(1、2等)

基本上,我希望每个单元格都像这样:

 _____________________________________
| (IMAGE) (TEXT)                      | --CELL 0
--------------------------------------
 _____________________________________
| (IMAGE) 2                           | --CELL 1
--------------------------------------

请原谅我粗俗的解释。:)

UITableViewCell对象具有属性;您只需设置此图像视图的图像,它就会自动将图像显示在正确的位置,并在其旁边显示标题。

您可以将图像和文本添加到UITableViewController子类中的UITableViewCell中,如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    [[cell imageView] setImage:anImage];
    [[cell textLabel] setText:[NSString stringWithFormat:@"%d",[indexPath row]];

     return cell;
}
UITableViewCell有一些“内置”属性可供利用,例如imageView、textLabel和detailTextLabel。有关更多信息,请查看。

在这里:

cell.imageView.image = [UIImage imageNamed:@"image.png"];
斯威夫特:

cell.imageView?.image = UIImage(named: "image.png")

第一个答案的Swift 2.0版本为:

cell.imageView?.image = UIImage(named: "image.png")

这个问题应该在我2015年9月24日编辑的答案中解决。
cell.imageView?.image = UIImage(named: "image.png")