2012-04-17 85 views
0

我正在开发一个应用程序,并坚持一件事。我试图自己解决我的问题,但无法弄清楚。如果有人知道如何解决这个问题,请帮助我。TableView行与iPhone中的复选框

我在每一行都创建了一个带有复选框的UITableView。当点击任何复选框时,我只想选中该复选框,但当前仅选中最后一个复选框。我已经在下面提供了我的当前代码,请让我知道是否看到任何应该更改的内容。

谢谢。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    cell.textLabel.text = [selectPlayer objectAtIndex:indexPath.row]; 
    checkboxButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 8, 25, 25)]; 
    checkboxButton.tag = indexPath.row; 
    [checkboxButton setTitle:nil forState:UIControlStateNormal]; 
    [checkboxButton addTarget:self action:@selector(checkboxButton:)forControlEvents:UIControlEventTouchUpInside]; 

    [checkboxButton addSubview:checkImage]; 
    cell.accessoryView = checkboxButton; 
    return cell; 
} 

-(void)checkboxButton:(id)sender { 
    NSIndexPath *indexPath = [selectTableView indexPathForCell:(UITableViewCell *)[[sender superview] superview]]; 
    switch (indexPath.section) { 
     case 0: 
      switch (indexPath.row) { 
       case 0: 
        [checkImage setImage:[UIImage imageNamed:@"checkbox-checked.png"]]; 
        NSLog(@"button pressed at section %d and %d row", indexPath.section, indexPath.row); 
        break; 

       case 1: 
        //[checkImage setImage:[UIImage imageNamed:@"checkbox-checked.png"]]; 
        NSLog(@"button pressed at section %d and %d row", indexPath.section, indexPath.row); 
        break; 

       default: 
        break; 
      } 
      break; 

     default: 
      break; 
    } 
} 
+0

如果我理解你的权利,你必须在每一个小区的按钮实现代码如下。一旦你点击一个特定单元格的按钮,只有这个图像应该改变。 您是否在NSLog输出中获得正确的节和行值? – user944351 2012-04-17 14:05:35

+0

这将导致问题,因为每当单元格向下滚动向下滚动再次,tableview将不会创建新的单元格,它会重用已分配的单元格,所以这将是问题,因此存储复选框的值在某些数据结构,如数组或字典,当点击复选框时,在数组结构中设置数值当从数据结构中获取值 – 2012-04-17 18:37:28

+0

@ user944351否我没有得到正确的部分并且它总是显示第0节第0行这就是为什么我没有得到 – vishiphone 2012-04-18 03:39:17

回答

0

我觉得你的问题是,你没有使用正确的方法,我认为你需要在这里实现

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath{ } 

你有indexPath对于选择的行,和的实例tableView,你需要做的就是实例化TableViewCell并检查你的选中标记。

+0

亲爱的马科斯我知道这种方法可以请你告诉我为什么目的和我将从我的上面的代码粘贴在这个方法中的代码。 – vishiphone 2012-04-19 03:44:11

+0

此方法的目的是处理选中或点击单个行时的情况。当选择发生时,会调用一些不同的方法,这是用于处理所选行更新的最常用方法。您应该从提供的indexPath中获取选定的行,并在此时将图像添加到该行。 – Chad 2012-04-19 15:59:33

0

声明复选框按钮内cellForRowAtIndexPath方法:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath