1. 程式人生 > >UITableView設定單元格選中後只顯示一個打勾的三種簡單方法(僅供參考)

UITableView設定單元格選中後只顯示一個打勾的三種簡單方法(僅供參考)

1、種方法:先定位到最後一行,若選中最後一行直接退出,否則用遞迴改變上次選中的狀態,重新設定本次選中的狀態。

- (UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath

  current=indexPath.row;

}

- (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

if(indexPath.row==current

){     

       return;

    }

   UITableViewCell*newCell =[tableViewcellForRowAtIndexPath:indexPath];

if(newCell.accessoryType==UITableViewCellAccessoryNone)

    {

       newCell.accessoryType=UITableViewCellAccessoryCheckmark;

      newCell.textLabel.textColor=[UIColorblueColor];

    }

   NSIndexPath

*oldIndexPath=[NSIndexPathindexPathForRow:current

inSection:0];

   UITableViewCell*oldCell =[tableViewcellForRowAtIndexPath:oldIndexPath];

if(oldCell.accessoryType==UITableViewCellAccessoryCheckmark)

    {

       oldCell.accessoryType=UITableViewCellAccessoryNone;

      oldCell.textLabel.textColor=[UIColorblackColor

];

    }

  current=indexPath.row;

}

2、第二種方法設定一個全域性變數,選中的時候傳值,然後通過重新載入資料,使得在選中這行打勾,其他行無樣式,此方法載入的時候第一行預設打勾了

-(void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath

{

    current=indexPath.row;

   [self.tableView reloadData];

}

- (UITableViewCellAccessoryType)tableView:(UITableView*)tableViewaccessoryTypeForRowWithIndexPath:(NSIndexPath*)indexPath

{

   if(current==indexPath.row&&current!=nil)

   {

returnUITableViewCellAccessoryCheckmark;

   }

   else

   {

returnUITableViewCellAccessoryNone;

   }

}

或者直接在

-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath裡面設定

單元格的預設高度為44

NSLog(@"%@",NSStringFromCGRect(cell.frame));

設定選中時的背景顏色可以用selectedbackgroundview設定

3、種方法:在選中時先遍歷整個可見單元格,設定所有行的預設樣式,再設定選中的這行樣式,此方法不能取消單元格的選中

-(void)tableView:(UITableView*)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath

{

      NSArray *array =[tableViewvisibleCells];

    for (UITableViewCell *cell in array) {

       [cellsetAccessoryType:UITableViewCellAccessoryNone];

      cell.textLabel.textColor=[UIColorblackColor];

    }

   UITableViewCell *cell=[self.tableViewcellForRowAtIndexPath:indexPath];

   cell.textLabel.textColor=[UIColorblueColor];

   [cell setAccessoryType:UITableViewCellAccessoryCheckmark];

}

此時只設定了在可見範圍內選擇的是一行,還得設定滾動後的選中狀態,

-(void)tableView:(UITableView*)tableViewwillDisplayCell:(UITableViewCell*)cellforRowAtIndexPath:(NSIndexPath*)indexPath

{

 NSIndexPath *index=[tableView indexPathForSelectedRow];

       if (index.row==indexPath.row&&index!=nil)

       {

          cell.backgroundColor=[UIColor colorWithRed:232.0/255.0 green:232.0/255.0blue:232.0/255.0 alpha:1.0];

          cell.textLabel.textColor=[UIColor colorWithRed:0.0 green:206.0/255.0blue:192.0/255.0 alpha:1.0];

       }

       else

       {

         cell.backgroundColor=[UIColor clearColor];

         cell.textLabel.textColor=[UIColor blackColor];

       }

}

單元格是否相同需要用到比較方法

NSIndexPath*index=[tableViewindexPathForSelectedRow];

NSComparisonResult result=[indexPathcompare:index];