gpt4 book ai didi

iphone - UITableview 的每个单元格中的不同图像

转载 作者:搜寻专家 更新时间:2023-10-30 19:51:54 25 4
gpt4 key购买 nike

我想为表格 View 的每个单元格设置不同的图像。我不知道该怎么做 - 请帮助我。

最佳答案

  1. 创建一个属性来存储不同图像名称的数组。

    在您的 header (.h) 文件中:

    @interface MyViewController : UITableViewController {
    NSArray *cellIconNames;
    // Other instance variables...
    }
    @property (nonatomic, retain) NSArray *cellIconNames;
    // Other properties & method declarations...
    @end

    在您的实现 (.m) 文件中:

    @implementation MyViewController
    @synthesize cellIconNames;
    // Other implementation code...
    @end
  2. 在您的 viewDidLoad 方法中,将 cellIconNames 属性设置为包含不同图像名称的数组(按照它们希望出现的顺序):

    [self setCellIconNames:[NSArray arrayWithObjects:@"Lake.png", @"Tree.png", @"Water.png", @"Sky.png", @"Cat.png", nil]];
  3. 在您的 tableView:cellForRowAtIndexPath: TableView 数据源方法中,获取与单元格行对应的图像名称:

    NSString *cellIconName = [[self cellIconNames] objectAtIndex:[indexPath row]];

    然后创建一个UIImage对象(使用cellIconName来指定图片)并将cell的imageView设置为这个UIImage对象:

    UIImage *cellIcon = [UIImage imageNamed:cellIconName];
    [[cell imageView] setImage:cellIcon];

在第 3 步之后,您的 tableView:cellForRowAtIndexPath: 方法将如下所示:

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

/* Initialise the cell */

static NSString *CellIdentifier = @"MyTableViewCell";

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

/* Configure the cell */

NSString *cellIconName = [[self cellIconNames] objectAtIndex:[indexPath row]];
UIImage *cellIcon = [UIImage imageNamed:cellIconName];
[[cell imageView] setImage:cellIcon];

// Other cell configuration code...

return cell;
}

关于iphone - UITableview 的每个单元格中的不同图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1851223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com