Iphone 如何在表视图中维护行的选择状态?
我是xcode的初学者。我正在创建一个带有多行滚动的问卷类型列表。但当我选择一行并滚动时,它不会保留其状态,当我返回时,它也会丢失其选择。所以任何人都请告诉我如何做到这一点,我已经尝试过这样的方法Iphone 如何在表视图中维护行的选择状态?,iphone,ios,objective-c,uitableview,Iphone,Ios,Objective C,Uitableview,我是xcode的初学者。我正在创建一个带有多行滚动的问卷类型列表。但当我选择一行并滚动时,它不会保留其状态,当我返回时,它也会丢失其选择。所以任何人都请告诉我如何做到这一点,我已经尝试过这样的方法 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if(val==1) { checkedArr=[[NSMutableArray all
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(val==1)
{
checkedArr=[[NSMutableArray alloc] init];
for (int i = 0; i<17; i++)
{
[checkedArr addObject:@"1"];
}
NSLog(@"Checked arr size %i",[checkedArr count]);
return 17;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%i",indexPath.row];
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil)
{
cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:14.0];
}
cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18.0];
UIView *selectionColor = [[UIView alloc] init];
selectionColor.backgroundColor = [UIColor colorWithRed:99/255.0f green:209/255.0f blue:248/255.0f alpha:1.0];
cell.selectedBackgroundView = selectionColor;
if([[checkedArr objectAtIndex:indexPath.row] isEqual:@"0"])
{
cell.accessoryView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tick.png"]];
NSLog(@"checkedArr 0000000");
}
else if ([[checkedArr objectAtIndex:indexPath.row] isEqual:@"1"])
{
cell.accessoryView=nil;
NSLog(@"checkedArr 111111");
}
cell.textLabel.frame=CGRectMake(75.0, 50.0, 150.0, 20.0);
cell.textLabel.text=[listArray objectAtIndex:indexPath.row];
NSLog(@"Checked arr size %i",[checkedArr count]);
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
cell=[questionTable cellForRowAtIndexPath:indexPath];
[checkedArr replaceObjectAtIndex:indexPath.row withObject:@"0"];
if([[checkedArr objectAtIndex:indexPath.row] isEqual:@"0"])
{
cell.accessoryView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tick.png"]];
NSLog(@"checkedArr 0000000");
}
else if ([[checkedArr objectAtIndex:indexPath.row] isEqual:@"1"])
{[questionTable deselectRowAtIndexPath:indexPath animated:YES];
cell.accessoryView=nil;
NSLog(@"checkedArr 111111");
}
NSLog(@"Val is %i",val);
NSLog(@"selected is %@",[listArray objectAtIndex:indexPath.row]);
// NSLog(@"Checked arr descripton %@",[checkedArr description]);
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView cellForRowAtIndexPath:indexPath].accessoryView = UITableViewCellAccessoryNone;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
如果(val==1)
{
checkedArr=[[NSMutableArray alloc]init];
对于(int i=0;i首先,每当您上下滚动tableView时,您的cellforrowatinexpath
方法都会创建新的单元格,这对内存管理非常不利
只需在您的cellForRowAtIndexPath方法中删除[questionTable取消rowatindexpath:indexath动画:YES]
,在我的应用程序中,我使用了与accessoryType相同的代码作为复选标记,请选中此项
将其放入.h文件可变数组arSelectedRows中
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
//Do anything you want for cell here
if([arSelectedRows containsObject:indexPath]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if(cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[arSelectedRows addObject:indexPath];
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
[arSelectedRows removeObject:indexPath];
}
NSLog(@"arSelectedRows are :%@",arSelectedRows);
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
编辑
//if you want only single check mark
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[arSelectedRows removeAllObjects];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if(cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[arSelectedRows addObject:indexPath];
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
[arSelectedRows removeObject:indexPath];
}
NSLog(@"arSelectedRows are:%@",arSelectedRows);
[self.tableview reloadData];//Reload your tableview here
}
它将帮助您。我认为您需要使用以下api:-
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition
如果您使用的是动态单元格,并且使用诸如NSArray之类的类变量更新行,则可能已将变量设置为弱而不是强。您只需要复选标记是的。我使用了小图像而不是复选标记。因此,如果选择了一行,图像将显示在该行的右侧。请检查我的编辑答案以进行单个选择。是否有其他解决方案?非常感谢。如果我必须使用si怎么办单选而不是多选?多选就像魅力一样有效,但单选不起作用。在单选中,没有选择行。它不显示图像。