簡體   English   中英

更改tableView所選單元格圖像顯示多個選定的單元格圖像

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

我有帶單元格圖像的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;
    }
     }

如果您使用的是iOS 5.0或更高版本,那么如果設置得當,您可以呼吁allowMultipleSelection 將此設置為NO 否則,請使用–deselectRowAtIndexPath:animated:說來清除第一個選擇。 -tableView:didSelectRowAtIndexPath:實現中調用這些方法之一。

我認為,您可以像這樣在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];

請檢查是否有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM