简体   繁体   English

更改tableView所选单元格图像显示多个选定的单元格图像

[英]Changing tableView Cell Image on selection shows multple selected cell images

I have tableView with Cell Image i want to add selection image when any cell is selected it works fine but problem is that first you select rows one then row one is selected then you selected row2 then row2 along with row1 is selected it should be only row2 selected not both . 我有带单元格图像的tableView我想在选择任何单元格时添加选择图像,它可以正常工作,但问题是首先选择一行,然后选择第一行,然后选择了row2,然后选择了row2和row1,应该只能是row2不能同时选择。

   - (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;
    }
     }

If you're writing for iOS 5.0 and up, you can appeal to allowsMultipleSelection if you have this setup well. 如果您使用的是iOS 5.0或更高版本,那么如果设置得当,您可以呼吁allowMultipleSelection Set this to NO . 将此设置为NO Otherwise, use –deselectRowAtIndexPath:animated: say to clear the first selection. 否则,请使用–deselectRowAtIndexPath:animated:说来清除第一个选择。 Call either of these in your implementation of -tableView:didSelectRowAtIndexPath: . -tableView:didSelectRowAtIndexPath:实现中调用这些方法之一。

I think, you can set cell's background view in cellForRowAtIndexPath like this 我认为,您可以像这样在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;

And, in didSelectRowAtIndexPath , use this 并且,在didSelectRowAtIndexPath ,使用它

[tableView deselectRowAtIndexPath:indexPath animated:YES];

Please check whether this helps. 请检查是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM