我是xcode的初学者。我正在创建一个包含多行滚动的问卷类型列表。但是当我选择一行并滚动它并不保持其状态时,当我回来时,它也会失去它的选择。所以任何人都请让我知道如何实现这一点,我尝试过这样的事情,
- (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;
}
答案 0 :(得分:2)
在我的应用程序中,我使用相同的代码作为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
}
它会帮助你。
答案 1 :(得分:0)
首先,每次向上/向下滚动tableView时,cellForRowAtIndexPath
方法都会创建新单元格,这对内存管理来说非常糟糕。
只需在[questionTable deselectRowAtIndexPath:indexPath animated:YES]
方法
cellForRowAtIndexPath
即可
答案 2 :(得分:0)
我认为你需要在下面使用这个api: -
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition
答案 3 :(得分:0)
如果您正在使用动态单元格,并使用某种类变量(如NSArray)更新行,则可能已将变量设置为弱而不是强。