Ios 表视图滚动/出列可重用单元问题
我有一个表视图和一个自定义单元格。该单元格包含3个按钮(复选框类型)。在按钮上单击我需要更改的相应按钮文本(选中/取消选中)。 我实现了这一点,但当我单击顶部单元格上的第一个按钮并向下滚动时,底部的新单元格也有此复选标记,当我向后滚动到顶部时,复选标记将移动到下一个单元格。。如何解决这个问题 代码: 截图:Ios 表视图滚动/出列可重用单元问题,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个表视图和一个自定义单元格。该单元格包含3个按钮(复选框类型)。在按钮上单击我需要更改的相应按钮文本(选中/取消选中)。 我实现了这一点,但当我单击顶部单元格上的第一个按钮并向下滚动时,底部的新单元格也有此复选标记,当我向后滚动到顶部时,复选标记将移动到下一个单元格。。如何解决这个问题 代码: 截图: 这是一个典型的问题,您依赖UI来完成模型的工作。模型,你应该传递给你的UITableViewCell,以便它可以被构建,它会告诉你,它应该显示一个“X”还是一个“O”。因为您没有这样做,所
这是一个典型的问题,您依赖UI来完成模型的工作。模型,你应该传递给你的
UITableViewCell
,以便它可以被构建,它会告诉你,它应该显示一个“X”还是一个“O”。因为您没有这样做,所以最简单的解决方案是每次退出队列时简单地重置单元的状态 我认为您需要将状态存储在数组中,然后在
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
选中按钮1\u单击“确实更改状态”,但当退出ReuseableCell队列时,它将再次从cellForRowAtIndexPath加载。在RemoteCustomCell.m文件中,您应该实现
- (void)prepareForReuse
{
[super prepareForReuse];
cell.btnCheck1.selected = NO;
}
这样,每个被重用的单元格都会将其BTNCECK1.selected值设置为NO,当您将单元格加载到cellForRowAtIndexPath
中时,只有当单元格可见且您将其设置为该值时,才会将其设置为YES
但将所有值存储在NSMutableArray中是关键。不存在只将值存储在单元格中的情况,它们可以在无法预见的基础上重新使用。将值添加到数组中,并使用[myArray objectAtIndex:indexPath.row]
在单元格中打开这些值
例如:
在viewDidLoad的某个地方
NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:@"1", @"0", @"1", @"1", nil];
在您的cellforrowatinexpath
BOOL yesOrNo = [[myArray objectAtIndex:indexPath.row] boolValue];
然后将您的按钮设置为布尔值。似乎您已退出单元格问题队列。您可以实现如下所示的
cellforrowatinexpath
方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"RemoteCell";
RemoteCustomCell *cell = (RemoteCustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
NSArray *arrNib=[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
cell= (RemoteCustomCell *)[arrNib objectAtIndex:0];
}
[cell.btnCheck1 addTarget:self action:@selector(CheckButton1_Click:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"RemoteCell";
RemoteCustomCell *cell = (RemoteCustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
NSArray *arrNib=[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
cell= (RemoteCustomCell *)[arrNib objectAtIndex:0];
}
[cell.btnCheck1 addTarget:self action:@selector(CheckButton1_Click:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}