UITableViewCell點擊勾選狀態

今天做個表格,突然發現在選中某行時打勾,再次選中其它行時,上次選中的行的勾還在,不能自動消失。
於是試了以下3種方法:
1、// 第一種方法:在選中時先遍歷整個可見單元格,設置所有行的默認樣式,再設置選中的這行樣式,此方法不能取消單元格的選中

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

{

       NSArray *array = [tableView visibleCells];

    for (UITableViewCell *cell in array) {

        [cell setAccessoryType:UITableViewCellAccessoryNone];

        cell.textLabel.textColor=[UIColor blackColor];

    

    }

   UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];

    cell.textLabel.textColor=[UIColor blueColor];

    [cell setAccessoryType:UITableViewCellAccessoryCheckmark];

}

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

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(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=[tableView indexPathForSelectedRow];

NSComparisonResult result=[indexPath compare:index];

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

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

   current=indexPath.row;

}

 

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

{

 

if(indexPath.row==current){

        

        return;

        

    }

    UITableViewCell*newCell = [tableView cellForRowAtIndexPath:indexPath];

    

    if(newCell.accessoryType==UITableViewCellAccessoryNone)

    {

        

        newCell.accessoryType=UITableViewCellAccessoryCheckmark;

        

        newCell.textLabel.textColor=[UIColor blueColor];

        

    }

    

    NSIndexPath*oldIndexPath =[NSIndexPath indexPathForRow:current

    

    inSection:0];

    UITableViewCell*oldCell = [tableView cellForRowAtIndexPath:oldIndexPath];

    

    if(oldCell.accessoryType==UITableViewCellAccessoryCheckmark)

    {

        

        oldCell.accessoryType=UITableViewCellAccessoryNone;

        

        oldCell.textLabel.textColor=[UIColor blackColor];

        

    }

   current=indexPath.row;

}

3.//方法三:設置一個全局變量,選中的時候傳值,然後通過重新加載數據,使得在選中這行打勾,其他行無樣式,此方法加載的時候第一行默認打勾了

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

{

     current=indexPath.row; 

    [self.tableView reloadData];

}

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

{

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

    {

        return UITableViewCellAccessoryCheckmark;

    }

    else

    {

    return UITableViewCellAccessoryNone;

    }

}

或者直接在

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath裏面設置

單元格的默認高度爲44

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

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章