在iOS的UITableView中,原生提供在右側的元件有四種:

UITableViewCellAccessoryDisclosureIndicator

UITableViewCellAccessoryDetailDisclosureButton

UITableViewCellAccessoryCheckmark

UITableViewCellAccessoryDetailButton

 

若我們需要有自訂元件時,通常的作法是自訂一個UITableViewCell;不過若是單純更換右側的元件,可以利用setAccessoryView來達成目的。

UISwitchView* mySwitch = [[UISwitch alloc] initWithFrame:CGRectZero];

[mySwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];

[myTableViewCell setAccessoryView:mySwitch];

 

- (void)switchAction:(id)sender

{

    // do something

}

 

若此元件是需要對應到cell的情況下,如上例,則可加上對應的標記,如將indexPath.row設為switch的tag,即可在自訂元件觸發時對應到正確的資料上,再做我們想做的事。

arrow
arrow
    全站熱搜

    Eason 發表在 痞客邦 留言(0) 人氣()