【问题标题】:Cell reusability and row selection in table view in iOSiOS 表格视图中的单元格可重用性和行选择
【发布时间】:2013-06-11 11:53:28
【问题描述】:

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

现在任务是点击按钮后每个部分只选择一行,

我的代码工作受到打击,随之而来的问题是

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

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

【问题讨论】:

  • 为什么你使用 i++;? tableview 自动递增不需要使用 i++?
  • 有没有解决办法?
  • 你使用了最差的格式。我有尽可能多的编辑。请检查拼写和语法,以便有人能够理解。

标签: iphone uitableview customization


【解决方案1】:

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

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

希望对你有帮助。

【讨论】:

  • 我没有添加复选标记,我添加了按钮,这不会工作
猜你喜欢
  • 2020-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-31
相关资源
最近更新 更多