2

我正在创建一个UITableView,包含有关公司的信息。它有多个部分,我只用一个textlabel和一个detailtextlabel二维数组填充了大多数单元格。我希望用图像填充一个单元格(准确地说是第一部分的第二个单元格)。

我想到的第一件事是创建另一个二维数组,并用空对象填充它,除了一个图像。但是,这没有任何意义,我相信有更专业和合乎逻辑的方法可以做到这一点。

所以问题是:如何tableViewCell在一个tableview包含多个部分的图像中填充一个图像。任何帮助将不胜感激,在此先感谢您!

4

5 回答 5

2

你不需要创建一个多维数组。只需添加一个cellForRowAtIndexPathUITableView 数据源的签入方法

if(indexpath.section == 0)

并设置单元格内容视图的背景图像,或者您也可以创建一个新的 UIImageView 并将其作为子视图添加到单元格中。

于 2013-05-20T09:33:21.753 回答
1

如果您预计会有一些这样的异常,您可以创建一个异常字典,其中键是NSIndexPath对象,值是图像。然后tableView:cellForRowAtIndexPath:检查字典是否包含索引路径。如果是这样,请使用图像,否则转到您的数组获取数据。

于 2013-05-20T09:13:52.550 回答
0

您的问题并不少见,现在唯一要了解的是委托方法为您提供了有关显示的部分和行的足够信息。现在在这些方法中,您可以使用数据源并检查所需条件并构造/返回单元格。

这将是以下几行:

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

// When you get to the section where you want customisation create and return the cell accordingly
if(indexpath.section == 0) {

// create Special Cell

} else {

// Create Normal Cell

}

  return cell;

 }

不要忘记使用dequecellwithreuseindentifier方法重用创建的单元格。我相信你能弄清楚。

于 2013-05-20T09:18:03.430 回答
0
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0 && indexPath.row==1) {
        UIView *v=[[UIView alloc] initWithFrame:cell.frame];
        [v setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"image1.png"]]];
        cell.backgroundView=v;
    }else{
        UIView *v=[[UIView alloc] initWithFrame:cell.frame];
        [v setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"image2.png"]]];
        cell.backgroundView=v;//OR SET cell.backgroundView=nil;
    }
}
于 2013-05-20T10:42:16.063 回答
-1

您可以使用 -

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

  cell.imageView.image = [UIImage imageNamed:@"image.png"];

  return cell;

  }

我希望,它会帮助你。

于 2013-05-20T09:11:23.687 回答