Ios Can';t刷新UITableView中的图片
我正在尝试更改tableview中的图片。我正在使用Ios Can';t刷新UITableView中的图片,ios,objective-c,cocoa-touch,cocoa,Ios,Objective C,Cocoa Touch,Cocoa,我正在尝试更改tableview中的图片。我正在使用[myTableView重载数据]在另一种方法(即启动)中刷新我的TableView,我可以看到TableView中的其他元素正在刷新,但图像保持不变。我错过了什么 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier
[myTableView重载数据]
在另一种方法(即启动)中刷新我的TableView
,我可以看到TableView中的其他元素正在刷新,但图像保持不变。我错过了什么
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *lblName;
UIImageView *checkMark = [[UIImageView alloc] initWithFrame:CGRectMake(230, 0, 30, 50)];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(10, 1, 260, 70)];
backgroundView.backgroundColor = UIColorFromRGB(0x000000, 0.3);
backgroundView.userInteractionEnabled = YES;
checkMark.image = [UIImage imageNamed:@"V.png"];
checkMark.contentMode = UIViewContentModeCenter;
checkMark.backgroundColor = [UIColor clearColor];
[backgroundView addSubview:checkMark];
[checkMark release];
[cell.contentView addSubview:backgroundView];
[backgroundView release];
}
if (something)
{
checkMark.image = [UIImage imageNamed:@"halfleft.png"];
NSLog(@"something was done");
}
return cell;
}
重新加载数据将正确调用此方法 您的问题很简单,如果单元格被“重用”,那么“复选标记”上的指针就不好了。这样做:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIView *backgroundView;
UIImageView *checkMark;
if (cell == nil) {
checkMark = [[UIImageView alloc] initWithFrame:CGRectMake(230, 0, 30, 50)];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
backgroundView = [[UIView alloc] initWithFrame:CGRectMake(10, 1, 260, 70)];
backgroundView.backgroundColor = UIColorFromRGB(0x000000, 0.3);
backgroundView.userInteractionEnabled = YES;
backgroundView.tag = 555;
checkMark.image = [UIImage imageNamed:@"V.png"];
checkMark.contentMode = UIViewContentModeCenter;
checkMark.backgroundColor = [UIColor clearColor];
checkMark.tag = 666;
[backgroundView addSubview:checkMark];
[checkMark release];
[cell.contentView addSubview:backgroundView];
[backgroundView release];
} else {
backgroundView = (UIImageView *)[cell.contentView viewWithTag:555];
checkMark = (UIImageView *)[backgroundView viewWithTag:666];
}
if (something)
{
checkMark.image = [UIImage imageNamed:@"halfleft.png"];
NSLog(@"something was done");
}
return cell;
}
但是,您应该做一些更简单的事情(如果可能的话,减少子视图。或者将UITableViewCell子类化,使指针位于复选标记上;-)应该将“if(something)”之前的最后一个“}”移到“cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease]下面;”为什么呢?除非单元格为零,否则我不想继续添加子视图。但您需要在同一单元格上显示新图像吗?因此,您只需要创建一个单元格,如果它是nil。其他所有东西都应该在(cell==nil)块之外。如果我把它放在cell==nil块之外,经过几次UITableView滚动后,所有东西都会变得缓慢且无响应。您的代码可以工作,但有重复的响应。我可以看到
复选标记。图像
也被添加到其他单元格中。另外,你能解释一下这里使用的标签是什么吗?我不知道你的代码应该如何工作。但是在单元格创建中避免任何.image(在if(cell==nil){})。标记允许您标识视图,以便在重用视图时可以找到它们。因此,请从if(cell==nil)中删除任何“动态”内容,并在:-之后的代码中执行该操作。问题是,如果我在cell==nil之外添加子视图,则在几个UITableView滚动之后,所有内容都会变得缓慢且无响应。那会发生吗?也许这与我从未在viewDidLoad中发布myTable(另一个问题是…)不在其外部创建/添加子视图有关。但是设置文本和图像,这不会使你的表格滚动速度减慢太多。此方法(CellForRowatineXpath)用于创建和重用单元格。但是它也允许,用一些内容来填充它。这就是我在最初的问题中所做的。我在单元格==nil
内创建了子视图,并在外部更改了图片,但它不起作用。