-1

我必须使用行和部分组合创建表格视图,以及我使用按钮自定义的部分行。

现在,点击按钮后,每个部分中只应选择一行,

我的代码工作受到打击,问题是

  1. 在点击特定部分行的一个按钮时,其他部分行按钮显示选择,我认为单元格可重用性问题。

  2. 当我点击特定按钮时,如何对同一部分的其余按钮执行取消选择过程。

这是我的代码

- (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"];
    }
}

它显示我的复选框,显示所有行的多项选择。自动选择任何单元格也会选择其他单元格。

4

1 回答 1

1

您可以将索引([indexPath row])保存在 didSelectRowAtIndexPath: 方法下的值中。然后,您可以在返回 cellForRowAtIndexPath: 方法之前检查 [indexPath row] 是否等于您的值。勾选后可以添加如下块;

if (match) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
}

我希望这可以帮助你。

于 2013-06-11T13:49:09.270 回答