表视图中的不同图像?

发布于 2024-12-07 10:25:17 字数 1131 浏览 0 评论 0原文

我是该论坛的新手,希望得到一些帮助。我有一个表格视图,我可以通过单独页面上的不同按钮向其中添加单元格。我想让每个按钮添加一个新单元格,但具有不同的图像。例如。按钮 1 将添加一个单元格和一个名为 @"button1.png" 的图像,按钮 2 将添加一个单元格和一个名为 @"button2.png" 的图像,依此类推。但我无法将图像添加到单元格中。我可以在 cellForRow 中执行此操作,但不能在按钮方法中执行此操作。有人可以帮我吗?我已经在按钮中尝试过此操作:

编辑 用于添加单元格和图像的按钮:

- (IBAction)outlet1:(id)sender {
[cart.cells addObject:@"1"];
[cart.cell.imageView setImage:[UIImage imageNamed:@"paddle1.png"]];
}

cellForRow

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

cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault     
reuseIdentifier:CellIdentifier];
} 
//cell.imageView.image = [UIImage imageNamed:@"paddle1.png"];
[myTableView reloadRowsAtIndexPaths:indexPath.row   
withRowAnimation:UITableViewRowAnimationNone];

cell.textLabel.text = [cells objectAtIndex:indexPath.row];
return cell;
}

I am quite new to the forum and id love some assistance. I have a table view and im able to add cells to it via different buttons on a seperate page. I want to have each button add a new cell but with a different image. So for example. Button 1 will add a cell and an image named @"button1.png" button two will add a cell with an image named @"button2.png" and so on. But im having trouble adding the image to the cell. I can do it in the cellForRow, but not in a button method. Could somebody please help me? I have tried this in the buttons:

EDIT
button used to add cell and image:

- (IBAction)outlet1:(id)sender {
[cart.cells addObject:@"1"];
[cart.cell.imageView setImage:[UIImage imageNamed:@"paddle1.png"]];
}

cellForRow

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

cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault     
reuseIdentifier:CellIdentifier];
} 
//cell.imageView.image = [UIImage imageNamed:@"paddle1.png"];
[myTableView reloadRowsAtIndexPaths:indexPath.row   
withRowAnimation:UITableViewRowAnimationNone];

cell.textLabel.text = [cells objectAtIndex:indexPath.row];
return cell;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

菊凝晚露 2024-12-14 10:25:17

UITableView 渲染的方式是,每当它要渲染该行时,它都会请求该行的单元格,生成它需要的像素,然后忘记该单元格。由于单元格的大部分结构不会随行而变化,因此可以重复使用同一单元格对象来渲染多行,并且只需(例如)更改标签文本或 imageView 的图像。

因此,在呈现行后更改单元格不会更改该行的外观。您需要告诉表重新渲染该行:

[myTable reloadRowsAtIndexPaths:indexPathArray withRowAnimation:foo];

这将导致操作系统调用 tableView 的数据源委托 cellForRowAtIndexPath: 消息,然后需要创建和/或修改一个单元格,然后被渲染。

单元格不是行。单元格是用于渲染行的对象。

The way UITableView renders is that it asks for a cell for a row whenever it is about to render that row, generates the pixels that it needs, and then forgets about the cell. Since most of the structure of a cell doesn't change from row to row, there is the capability to re-use the same cell object for rendering multiple rows and just (eg) change label text or an imageView's image.

Hence, changing a cell after a row has been rendered doesn't change the appearance of the row. You need to tell the table to re-render that row:

[myTable reloadRowsAtIndexPaths:indexPathArray withRowAnimation:foo];

This will cause the OS to call the tableView's Data Source delegate cellForRowAtIndexPath: message, which then needs to create and/or modify a cell, which then gets rendered.

Cells aren't rows. Cells are objects used to render rows.

半世晨晓 2024-12-14 10:25:17

如果我理解这个问题,您希望购物车对象在表格单元格中显示其图片。如果这是真的,那么为什么不向购物车对象添加 UIImage 视图呢?在适当的时间(用户选择对象的某个地方)将项目的图像存储到对象的 UIImage 视图中。然后,当您将购物车对象设置到表中时,您将获得对象名称(大概)及其对象中​​的图像,然后您可以轻松地将单元格的图像 = 设置为对象图像。不要尝试在“cellForRowAtIndexPath”方法中显示要显示哪个图像的逻辑。将图像和所有属性与对象一起存储,并使用 objectAtIndex 指向完整对象并根据对象属性设置单元格。

If I understand the problem, you want a cart object to display its picture in a table cell. If this is true, then why don't you add an UIImage view to the cart object. At an appropriate time (somewhere where the object is select by the user) store the image of the item to your UIImage view of the object. Then when you go to set the cart object into your table, you have then objects name (presumably) and it's image in your object and you can then easily set the cell's image = to your objects image. Don't try to have the logic of which image to display inside the "cellForRowAtIndexPath" method. Store the image and all attributes with your object and use the objectAtIndex to point to your complete object and set your cell based on your objects attributes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文