Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Ios中滚动表格列表时加载重复数据_Ios_Objective C - Fatal编程技术网

在Ios中滚动表格列表时加载重复数据

在Ios中滚动表格列表时加载重复数据,ios,objective-c,Ios,Objective C,Hi new for iOS,在我的项目中,我使用自动布局以编程方式添加了一个表列表,因此一切正常 但当我滚动表格列表时,每个单元格中都加载了重复的内容 我在这里做错了什么?(我的意思是第一个单元格数据正在加载到所有单元格中) 我的代码:- 您可以在运行时和滚动期间创建UILabel、UIImageview和MKMapView,并创建每次创建的UILabel、UIImageview和MKMapView,并将其添加到Cell.contentView 因此,在以下if条件下进行此操作。希望这对你的工

Hi new for iOS,在我的项目中,我使用自动布局以编程方式添加了一个表列表,因此一切正常

但当我滚动表格列表时,每个单元格中都加载了重复的内容

我在这里做错了什么?(我的意思是第一个单元格数据正在加载到所有单元格中)

我的代码:-
您可以在运行时和滚动期间创建UILabel、UIImageview和MKMapView,并创建每次创建的UILabel、UIImageview和MKMapView,并将其添加到
Cell.contentView

因此,在以下if条件下进行此操作。希望这对你的工作有帮助

if (Cell == nil) {

        Cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

您可以在运行时和滚动期间创建UILabel、UIImageview和MKMapView,并创建每次创建的UILabel、UIImageview和MKMapView,并将其添加到
Cell.contentView

因此,在以下if条件下进行此操作。希望这对你的工作有帮助

if (Cell == nil) {

        Cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
在你

if (Cell == nil) {
    Cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
添加以下内容:

for (UIView *subView in cell.contentView.subviews) {
    [subView removeFromSuperview];
}
或者为添加到contentView的每个子视图指定一个标记,并在创建和再次添加该标记之前检查contentView中是否存在具有该标记的视图

例如:

if([cell.contentView viewWithTag:99]) {
    Personimage *personImage = [[UIImageView alloc]init];
    personimage.image = [UIImage imageNamed:@"ram.jpeg"];
    personImage.tag = 99;
    personImage.translatesAutoresizingMaskIntoConstraints = NO;
    [cell.contentView addSubview: personImage];
} else {
    Personimage *personImage = (Personimage *)[cell.contentView viewWithTag:99];
    personImage.image = [UIImage imageNamed:@"ram.jpeg"];
}
在你

if (Cell == nil) {
    Cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
添加以下内容:

for (UIView *subView in cell.contentView.subviews) {
    [subView removeFromSuperview];
}
或者为添加到contentView的每个子视图指定一个标记,并在创建和再次添加该标记之前检查contentView中是否存在具有该标记的视图

例如:

if([cell.contentView viewWithTag:99]) {
    Personimage *personImage = [[UIImageView alloc]init];
    personimage.image = [UIImage imageNamed:@"ram.jpeg"];
    personImage.tag = 99;
    personImage.translatesAutoresizingMaskIntoConstraints = NO;
    [cell.contentView addSubview: personImage];
} else {
    Personimage *personImage = (Personimage *)[cell.contentView viewWithTag:99];
    personImage.image = [UIImage imageNamed:@"ram.jpeg"];
}


我希望在目标cadd中找到解决方案,在if条件中创建并添加标签代码。你说我不明白是什么意思,而且我在if条件中添加了标签代码,比如([Cell.contentView addSubview:Personimage];)每次将单元格出列时都会重复约束,这不是一个好主意。我希望在目标cadd中找到解决方案,并在if条件中创建和添加标签代码。什么意思我不理解,而且我在if条件中添加了标签代码,如([cell.contentView addSubview:Personimage];)每次将单元格出列时都会重复约束,这不是一个好主意。我只想在第1行no meter中添加该字段,但您必须将此代码与我一样放在if条件的旁边said@Krish若问题已解决,你们必须接受答案,以便其他人也注意到。单元格标识符对于每个单元格不应是唯一的。拥有单元标识符的全部目的是实现单元的可重用性。拥有唯一的单元格ID无法达到这一目的。@Krish在alloc图像代码之后在side cellforrowIndex代码中生成四舍五入代码。与ViewDidAppari不同的是,我只想在第1行no meter中添加字段,但您必须将此代码放在if条件的旁边,如我所示said@Krish若问题已解决,你们必须接受答案,以便其他人也注意到。单元格标识符对于每个单元格不应是唯一的。拥有单元标识符的全部目的是实现单元的可重用性。拥有唯一的单元格ID无法达到这一目的。@Krish在alloc图像代码之后在side cellforrowIndex代码中生成四舍五入代码。取而代之的是ViewDidAppearth删除UIView,如果有的话,但是OP添加了UILabel图像和映射,所以这个for循环没有帮助你需要che is subview isKindOf Label,然后删除它isKindOf UIImage,然后删除它isKindOf map,然后删除它,我认为这是内存消耗。所有这些都是UIView的子类,所以它适用于所有情况。:)内存消耗还取决于他添加到contentView的子视图的数量。对于问题中添加的内容应该没有问题。如果有,请删除UIView,但OP添加了UILabel图像和贴图,因此此for循环没有帮助您需要che is subview is kindof Label,然后删除它是UIImage的一部分,然后删除它是map的一部分,然后删除它,我认为这是内存消耗。所有这些都是UIView的子类,所以它适用于所有方面。:)内存消耗还取决于他添加到contentView的子视图的数量。问题中添加的内容应该可以。