如何在UITableview中选择多行

时间:2012-01-02 07:59:04

标签: iphone cocoa-touch

我想在UITableview中选择多行。我可以选择,但我的问题是当我滚动UITableView时,会自动选择特定行。 我正在使用此代码:

-(void)tableView: (UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

    if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) {
        [selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];


    } 
    else {
        [selectedCell setAccessoryType:UITableViewCellAccessoryNone];


    }

}

2 个答案:

答案 0 :(得分:3)

进一步的代码用于UITableview中的多项选择

#import "RootViewController.h"

@implementation RootViewController

@synthesize arForTable = _arForTable;
@synthesize arForIPs = _arForIPs;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.arForTable=[NSArray arrayWithObjects:@"Object-One",@"Object-Two",@"Object-Three",@"Object-Four",@"Object-Five", nil];
    self.arForIPs=[NSMutableArray array];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.arForTable count];
}

- (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] autorelease];
    }
    if([self.arForIPs containsObject:indexPath]){
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    } else {
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }
    cell.textLabel.text=[self.arForTable objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if([self.arForIPs containsObject:indexPath]){
        [self.arForIPs removeObject:indexPath];
    } else {
        [self.arForIPs addObject:indexPath];
    }
    [tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
    [super viewDidUnload];
}



- (void)dealloc {
    [super dealloc];
}

@end

有关重新选择多项选择的详情,请参阅following link here.

答案 1 :(得分:1)