iOS 表格视图单元格处理
我有这段代码,其中“lineaRedToday”是一个 UIImageView:
- (void)viewDidLoad {
[super viewDidLoad];
lineaRedToday = [UIImage imageNamed:@"line4.png"];}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyIdentifier";
MyIdentifier = @"tblCellView";
TableViewCell *cell = (TableViewCell*)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil];
cell = tblCell;
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[[lineDay objectAtIndex:currentTodaylineRed -1] setImage:lineaRedToday];
return cell;
- (IBAction) change{
lineaRedToday = [UIImage imageNamed:@"linea.png"];
[lineDay setImage:lineaRedToday];
[myTableView reloadData];
}
它是一个带有 15 个自定义 UITableViewCell 的 UITableView,我想在 UIImageView 上更改图像,此 ImageView 是“lineDay”,lineDay 存在于所有单元格中,我想要更改它在所有单元格中的图像,但 IBAction“更改”仅在最后一个单元格中更改 UIImageView,而根本没有更改......为什么?
I have this code, where "lineaRedToday" is an UIImageView:
- (void)viewDidLoad {
[super viewDidLoad];
lineaRedToday = [UIImage imageNamed:@"line4.png"];}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyIdentifier";
MyIdentifier = @"tblCellView";
TableViewCell *cell = (TableViewCell*)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil];
cell = tblCell;
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[[lineDay objectAtIndex:currentTodaylineRed -1] setImage:lineaRedToday];
return cell;
- (IBAction) change{
lineaRedToday = [UIImage imageNamed:@"linea.png"];
[lineDay setImage:lineaRedToday];
[myTableView reloadData];
}
It is a UITableView with 15 custom UITableViewCell and I want to change the image at an UIImageView, this ImageView is "lineDay", lineDay is present in all cells, and I want change its image at all cells, but the IBAction "change" change the UIImageView only in the last cell and not at all....why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,它会更改最后一个单元格中的图像,因为它只引用最后一个单元格,如果您想这样做,那么当您在 cellForRowAtIndexPath 中创建单元格时,检查 BOOL 并根据该 BOOL 设置图像。另外,在 IBAction 中更改该 BOOL 值并在表视图上调用 reloadData 来重新加载它。作为 -
yes it will change the image in last cell because it has the reference of only last cell, if you want to do this then when you are creating cell in cellForRowAtIndexPath check for a BOOL and set image according that BOOL. Also in IBAction change that BOOL value and call reloadData on table view to reload it. As -
首先,lineaRedToday 是一个
UIImage
,而不是UIImageView
。第一个是图像,第二个是视图层次结构中的对象。然后,在您的代码中
使用
tblCell
。我看不出这是什么。您应该从刚刚从笔尖加载的内容中分配一些内容。我不知道
lineDay
到底是什么,但单个视图(即UIImageView
)只能出现在一个单元格中,而不是出现在许多单元格中。如果您更改图像 (lineaRedToday
),您仍然需要在视图中设置此新图像。 应该有类似的内容。配置单元时
First, lineaRedToday is an
UIImage
, not anUIImageView
. The first is an image, the second an object in the view hierarchy.Then, in your code
you use
tblCell
. I cannot see what this is. You should assign something from what you were just loading from the nib.I don't know what
lineDay
is exactly, but a single view (i.e.UIImageView
) can only be present in one cell, not in many. If you change the image (lineaRedToday
), you still have to set this new image in your views. There should be something likewhen configuring the cell.