我有使用Cell Image的tableView我想在选择任何单元格时添加选择图像它工作正常但问题是首先选择行1然后选择第一行然后选择row2然后row2和row1被选中它应该只选择第二行而不是两者。
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
cell = [self.tableView cellForRowAtIndexPath:indexPath];
if (indexPath.row==0) {
cell.imageView.image=[UIImage imageNamed:@"Activity2.png"];
}
else if(indexPath.row==1){
cell.imageView.image=[UIImage imageNamed:@"Catalog2.png"];
}
else{
cell.imageView.image=[UIImage imageNamed:@"Library2.png"];
}
NSUInteger row = indexPath.row;
[self.appDelegate.splitViewController viewWillDisappear:YES];
NSMutableArray *viewControllerArray=[[NSMutableArray alloc] initWithArray: [[self.appDelegate.splitViewController.viewControllers objectAtIndex:1] viewControllers]];
[viewControllerArray removeLastObject];
if (row == 0) {
self.firstDetailViewController=[[[FirstDetailViewController alloc] init]autorelease];
[viewControllerArray addObject:self.firstDetailViewController];
self.appDelegate.splitViewController.delegate = self.firstDetailViewController;
}
if (row == 1) {
self.secondDetailViewController=[[[SecondDetailViewController alloc]init]autorelease];
[viewControllerArray addObject:self.secondDetailViewController];
self.appDelegate.splitViewController.delegate = self.secondDetailViewController;
}
if (row == 2) {
self.myLearningViewController=[[[MyLearningViewController alloc]init]autorelease];
[viewControllerArray addObject:self.myLearningViewController];
self.appDelegate.splitViewController.delegate = self.myLearningViewController;
}
}
答案 0 :(得分:0)
如果你正在为iOS 5.0及更高版本编写,如果你有这样的设置,你可以诉诸allowsMultipleSelection。将其设置为NO
。否则,请使用–deselectRowAtIndexPath:animated:来清除第一个选择。在-tableView:didSelectRowAtIndexPath:
。
答案 1 :(得分:0)
我认为,你可以在cellForRowAtIndexPath
这样设置单元格的背景视图
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundColor = [UIColor clearColor];
UIImageView *bgView= [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
bgView.backgroundColor = [UIColor clearColor];
bgView.image = [UIImage imageNamed:@"cellBgImage.png"];
cell.backgroundView = bgView;
而且,在didSelectRowAtIndexPath
中,请使用此
[tableView deselectRowAtIndexPath:indexPath animated:YES];
请检查这是否有帮助。