iOS中UITableView單選標記的實現

摘錄自《iPhone3開發基礎教程》第202頁:

#pragma mark -

#pragma mark Table Delegate Methods

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

{

    int newRow = [indexPath row];

    int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;

    if(newRow != oldRow)

    {

        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];

        newCell.accessoryType = UITableViewCellAccessoryCheckmark;

        

        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastIndexPath];

        oldCell.accessoryType = UITableViewCellAccessoryNone;

        lastIndexPath = indexPath;

    }

    

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

 

運行結果:要是點的是同一行,選中標記就不變。要是另外選擇了一行,該行就設上“對鉤”,原來那一行就沒有標記了。


轉自: http://hi.baidu.com/jt_one/item/bc915f31e6e12e21b2c0c5eb

發佈了1 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章