ios-UITableViewCell在滚动时显示错误的数据
这是我的密码:ios-UITableViewCell在滚动时显示错误的数据,ios,objective-c,uitableview,Ios,Objective C,Uitableview,这是我的密码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifierNormal = @"CellNormal"; static NSString *CellIdentifierTracking = @"CellTracking"; sw
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifierNormal = @"CellNormal";
static NSString *CellIdentifierTracking = @"CellTracking";
switch (self.mapState) {
case MapStateNormal:
{
// UITableViewCell *cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifierNormal];
// if (cell == nil) {
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierNormal];
// }
[cell.textLabel setText:@"abc"];
return cell;
}
case MapStateTracking:
{
// UITableViewCell *cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifierTracking];
// if (cell == nil) {
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierTracking];
// }
if (indexPath.row == 0) {
[cell.textLabel setText:@"Lorem ipsum"];
} else {
NSURL *url = [LoremIpsum URLForPlaceholderImageFromService:LoremIpsumPlaceholderImageServicePlaceKittenCom
withWidth:1024
height:1024];
[cell.imageView setImageWithURL:url
placeholderImage:[UIImage imageNamed:@"MenuButton"]];
}
return cell;
}
default:
break;
}
return nil;
}
这段代码工作正常,但不是最佳做法,因为我每次都重新创建UITableViewCell
。它显示如下:
但是,当我取消对上面这些行的注释以启用dequeueReusableCell
时,该表将显示其单元格,其中包含如下错误(黄色部分是我的代码):
您可以看到,在第一行中有一个UIImage
,在下面的一些行中有文本,而我显然没有在代码中设置它
我能做些什么来解决这个问题?还是我应该坚持第一种方法
谢谢。只需设置
[cell.textlab setText:@”“代码>对于不想显示任何文本的每个单元格。单元格与上一个文本一起重复使用,因此您需要将其清除。您应该真正重复使用表格视图单元格,因为如果您一直重新创建它们,会产生很大的开销,即注释错误的代码是正确的。
接下来,用户可以说:“当重用单元格时,tableView:cellforrowatinexpath:
中的表视图的委托应该总是重置所有内容。”
如果不重置内容,它将再次显示。
所以我建议你
cell.imageView.image = nil;
在您的-(UITableViewCell*)表视图中:CellForRowatineXpath:(NSIndexPath*)indexPath
方法。尝试此操作,如果它是单元格,请清除该单元格!=无
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifierNormal = @"CellNormal";
static NSString *CellIdentifierTracking = @"CellTracking";
switch (self.mapState) {
case MapStateNormal:
{
UITableViewCell *cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifierNormal];
if (cell == nil) {
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierNormal];
} else {
[cell.textLabel setText:@""];
[cell.imageView setImage:nil];
}
...
return cell;
}
case MapStateTracking:
{
UITableViewCell *cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifierTracking];
if (cell == nil) {
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierTracking];
} else {
[cell.textLabel setText:@""];
[cell.imageView setImage:nil];
}
....
return cell;
}
default:
break;
}
return nil;
}
好的,谢谢。我认为重设内容是一种自动操作,每当要重用单元格时都会执行。