用户选择行后,更改分区tableView中的行的accessoryType

时间:2011-11-14 16:36:11

标签: iphone objective-c ios uitableview

我有一个分区的tableView,我希望我的用户从表中选择一个项目。当他们选择项目时,项目旁边应显示一个检查(使用UITableViewCellAccessoryCheckmark)。如果他们进行了先前的选择,则应从先前选择的行中删除该检查。这是我正在使用的代码:

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

    int newRow = [indexPath row];

    int oldRow = [lastIndexPath row];

    if (newRow != oldRow || newRow == 0)
    {

        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;

        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath]; 
        oldCell.accessoryType = UITableViewCellAccessoryNone;

        [lastIndexPath release];
        lastIndexPath = indexPath;  
    }

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

lastIndexPath.h文件中私有声明。

此代码适用于未分区的小型列表。但是在一个分区的大表中,它会在其他部分的行中添加随机复选标记。这几乎就好像cellForRowAtIndexPath忽略了indexPath中的部分。

如果我选择的行大于最小部分中的行数,代码也会崩溃。

以下是cellForRowAtIndexPath的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger section = [indexPath section];

    NSUInteger row = [indexPath row];

    NSString *key = [keys objectAtIndex:section];

    NSArray *itemSection = [items objectForKey:key];

    static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SectionsTableIdentifier] autorelease];

    }
    NSArray *rowLabel = [itemSection objectAtIndex:row]; 

    cell.textLabel.text = [rowLabel objectAtIndex:1];

    NSString *detText = [rowLabel objectAtIndex:0];

    detText = [detText stringByAppendingString:@"            $"];

    detText = [detText stringByAppendingString:[rowLabel objectAtIndex:2]];

    cell.detailTextLabel.text = detText;

    return cell;
}

2 个答案:

答案 0 :(得分:0)

您在这里遇到的一个问题是如何在lastIndexPath中保存indexPath。您需要保留您在lastIndexPath中保存的indexPath。传递给此方法的indexPath是自动释放的,因此如果您不保留它,它可能会从您下面释放出来。这可能会导致您的崩溃。

答案 1 :(得分:0)

这可能正是您正在寻找的。我为我的一个应用程序开发了这个。请享用 ! (如果是,请标记为已回答) 另请注意,我正在使用ARC,因此没有保留和释放。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   /* 
    1. First we get the indexPath from the prior priorSelectedRowInteger and priorSelectedSectionInteger ivars. Note: we could use a single indexPath ivar, but we separate them into row and section here for clarity.
    2. Then we reset the selectedRowInteger ivar to the currently selected row. This must be done before any rows are reloaded.
    3. Then we reload only the two rows at the concerned index paths, as we have captured the indexPath of the prior selected row and the method gives us the new one.  We could just simply reload the table here with [self.tableView reloadData], but it would not be animated and not as smooth.

    */
    NSIndexPath *priorSelectedIndexPath = [NSIndexPath indexPathForRow:priorSelectedRowInteger inSection: priorSelectedSectionInteger];
     // Now that we have the priorSelectedIndexPath, we save the new one for the next round.
     self.priorSelectedRowInteger = indexPath.row; 
     self.priorSelectedSectionInteger = indexPath.section;

    // For a changing tableView, check to make sure the priorIndexPath is still valid before trying to reload the prior row.
   // NSLog(@"priorSelectedIndexPath %@", priorSelectedIndexPath);
     if ((tableView.numberOfSections >= priorSelectedIndexPath.section+1) && ([tableView numberOfRowsInSection:priorSelectedIndexPath.section] >= priorSelectedIndexPath.row+1)) {
    NSArray *thePriorIndexPathArray = [NSArray arrayWithObject:priorSelectedIndexPath];
   [self.tableView reloadRowsAtIndexPaths:thePriorIndexPathArray withRowAnimation:UITableViewRowAnimationFade];
}


    // Reload only the selected indexPath - necessary to update the text colors etc.
    NSArray *theIndexPathArray = [NSArray arrayWithObject:indexPath];
    [self.tableView reloadRowsAtIndexPaths:theIndexPathArray withRowAnimation:UITableViewRowAnimationFade];
}