我必须使用行和节组合创建表视图,并使用按钮自定义节的行。
现在任务只需在点击按钮
后在每个部分中选择一行我的代码工作受到了打击,问题就是
点击特定部分行的一个按钮,其他部分行按钮显示选择,我认为是细胞再利用性问题。
点击特定按钮时,如何对同一部分的其余按钮执行去除过程。
这是我的代码
- (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];
}
self.juju=[[NSMutableArray alloc]init];
NSArray *listSeprately = [[NSArray alloc]init];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"questionid==%d",indexPath.section+1];
listSeprately = [self.questAnswer filteredArrayUsingPredicate:predicate];
self.buttonMe=[[UIButton alloc]init];
self.buttonMe = [UIButton buttonWithType:UIButtonTypeCustom];
[self.buttonMe setFrame:CGRectMake(260.0, 7.0, 30.0, 30.0)];
self.buttonMe.tag=i;
if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"])
{
[self.buttonMe setImage:[UIImage imageNamed:@"check.gif"] forState:UIControlStateNormal];
}
else
{
[self.buttonMe setImage:[UIImage imageNamed:@"uncheck.jpg"] forState:UIControlStateNormal];
}
[self.buttonMe addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
cell.textLabel.text=[juju objectAtIndex:indexPath.row];
[cell.contentView addSubview: self.buttonMe];
i++;
return cell;
}
当我们点击任何按钮时,此代码调用。
-(void)buttonClicked:(id)sender
{
CGPoint touchPoint = [sender convertPoint:CGPointZero toView:self.tableView ];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:touchPoint];
// No need to use tag sender will keep the reference of clicked button
self.buttonMe = (UIButton *)sender;
//Checking the condition button is checked or unchecked.
//accordingly replace the array object and change the button image
if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"])
{
[ self.buttonMe setImage:[UIImage imageNamed:@"checkImage.jpg"] forState:UIControlStateNormal];
[arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:@"Check"];
}
else
{
[ self.buttonMe setImage:[UIImage imageNamed:@"uncheck.jpg"] forState:UIControlStateNormal];
[arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:@"Uncheck"];
}
[self.tableView reloadData];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.arrayCheckUnchek = [[NSMutableArray alloc]init];
self.questAnswer=//here i hve records store that is 19
for(int i=0; i<[self.questAnswer count]; i++)
{
[arrayCheckUnchek addObject:@"Uncheck"];
}
}
它显示我的复选框,显示所有行的多个选择。自动选择任何单元格,它也会选择其他单元格。
答案 0 :(得分:1)
您可以将索引([indexPath row])保存在didSelectRowAtIndexPath:方法下的值中。然后,您可以在返回cellForRowAtIndexPath:方法之前检查[indexPath行]是否等于您的值。执行检查后,可以添加以下块;
if (match) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
我希望这会对你有所帮助。