簡體   English   中英

iOS tableView多項選擇

[英]iOS tableView multiple selection

許多選擇多行的方法或方法已被棄用或不起作用。

我只想在didSelectRowAtIndexPath選擇多行並將其保存到Array或NSUserDefault 在這種情況下,我選擇了NSUserDefault但我願意將其更改為Array。

這是我的代碼:

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

    NotificationsTableViewCell *cell = (NotificationsTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];

    //set clicked icon after row selected
    cell.imgIcon.image=[UIImage imageNamed:@"select-icon"];

    //notification id for selected row
    Notifications* selected = notifications[indexPath.section];
    selectedGroup = selected.NotificationsId;

     //save it in nsuserdefaults
    [[NSUserDefaults standardUserDefaults] setObject:selectedGroup forKey:@"notificationSelectedID"];

}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{

//deselect the row

}

注意 :我正在使用最新的Xcode SDK 8。

謝謝您的幫助。

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

NotificationsTableViewCell *cell = (NotificationsTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];

 // do your logic with following condition

if (cell.accessoryType == UITableViewCellAccessoryNone) 
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

    } 
    else 
    {

        cell.accessoryType = UITableViewCellAccessoryNone;

    }

}

您可以聲明可變數組,並且每當用戶每次選擇特定行時,都為數組中的特定行添加所需的詳細信息,然后使用NSUserDefaults存儲更新后的數組。

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

[arrRows addObject:[NSString stringWithFormat:@"%@", indexPath]];
[[NSUserDefaults standardUserDefaults] setValue:arrRows forKey:@"arrRows"];

}

暫無
暫無

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

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