Ios 如何更改“UITableView”中自定义单元格中的“UIImageView”`
我在Ios 如何更改“UITableView”中自定义单元格中的“UIImageView”`,ios,uitableview,Ios,Uitableview,我在UITableView的自定义单元格中有UIImageView,我想在didSelectRowAtIndexPath 它在UITableView外部使用UIImageView,但在内部不工作 我正在使用这个代码 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSString *string = [[self.itemsInTable objec
UITableView
的自定义单元格中有UIImageView
,我想在didSelectRowAtIndexPath
它在UITableView
外部使用UIImageView
,但在内部不工作
我正在使用这个代码
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *string = [[self.itemsInTable objectAtIndex:indexPath.row] objectForKey:@"Name"];
ExpandCell *cell = [self.menuTableView dequeueReusableCellWithIdentifier:@"Cell"];
UIImage *image;
if ([string isEqualToString:@"News"]) {
if ([[self.viewImage image] isEqual:[UIImage imageNamed:@"down-arrow.png"]]) {
NSLog(@"down");
image = [UIImage imageNamed:@"up-arrow.png"];
} else {
NSLog(@"up");
image = [UIImage imageNamed:@"down-arrow.png"];
}
cell.imgExpand.image = image;
self.viewImage.image = image;
}
}
如何修复该问题?以下行将创建一个新单元格:
ExpandCell *cell = [self.menuTableView dequeueReusableCellWithIdentifier:@"Cell"];
将其更改为:
ExpandCell *cell = [tableView cellForRowAtIndexPath:indexPath];
这将为您提供您刚才选择的当前可见单元格。添加此命令
[tableView descrowatindexpath:indexPath animated:YES]
在didSelectRowAtIndexPath的末尾设置新图像后,可以使用以下功能:
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
重新加载您更改的单元格。首先为您的
UIImageView
(例如10)指定一个标记号。
然后在didSelectRowAtIndexPath
方法中编写此代码
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIImageView *yourImageView = (UIImageView*)[cell viewWithTag:10];//Replace yourImageView with the name of your UIImageView on custom cell
//--------- Your code ------
NSString *str = [[self.itemsInTable objectAtIndex:indexPath.row] objectForKey:@"Name"];
UIImage *image;
if ([str isEqualToString:@"News"])
{
if ([[self.viewImage image] isEqual:[UIImage imageNamed:@"down-arrow.png"]])
{
NSLog(@"down");
image = [UIImage imageNamed:@"up-arrow.png"];
}
else {
NSLog(@"up");
image = [UIImage imageNamed:@"down-arrow.png"];
}
[yourImageView setImage:image];
[self.viewImage setImage:image];
}
我认为这将解决您的问题。这与所问的问题无关?这应该作为评论发布,而不是回答。此外,OP可能不希望清除选择。我曾经在[]遇到过同样的问题。因此,我认为,这可能对我有用,因为它对我有用