[英]How to store multiple selected row items in mutable array in tableview
我已經編寫了用於顯示具有3個部分的tableview的代碼,現在我想將選定的行項目存儲到數組中,而當我嘗試下面的代碼時,我得到的數組值為nil。 建議我如何解決此問題下面我在添加代碼和Json示例數據
{ "technicians": { "group": [ { "first_name": "First", "last_name": "Available", "id": "" }, { "first_name": "idea", "last_name": "Chandra", "id": 2885 }, { "first_name": "manson", "last_name": "Tolios", "id": 2878 }, { "first_name": "tata", "last_name": "Masson", "id": 2869 } ], "client": [ { "first_name": "tsms", "last_name": "ysys", "id": 2875 }, { "first_name": "Oscar", "last_name": "tata", "id": 2851 }, { "first_name": "Peter", "last_name": "yaha", "id": 2873 }, { "first_name": "iaka", "last_name": "adad", "id": 2847 }, { "first_name": "taga", "last_name": "uaja", "id": 2917 } ], "contractors": [ { "first_name": "taha" }, { "first_name": "haka" }, { "first_name": "oala" } ] } }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"EcsStafflist"]; if(!cell) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"]; } UILabel *Fname = (UILabel *)[cell viewWithTag:18]; UILabel *Lname = (UILabel *)[cell viewWithTag:19]; Fname.text = [[[mydic objectForKey:[MysectionTitles objectAtIndex:indexPath.section]]objectAtIndex:indexPath.row]valueForKey:@"first_name"]; Lname.text = [[[mydic objectForKey:[MysectionTitles objectAtIndex:indexPath.section]]objectAtIndex:indexPath.row]valueForKey:@"last_name"]; if (indexPath.row %2 ==1) cell.backgroundColor = [UIColor colorWithRed:0.60 green:0.60 blue:0.95 alpha:1.0]; else cell.backgroundColor = [UIColor colorWithRed:0.64 green:0.89 blue:0.61 alpha:1.0]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { stringId = [[[mydic objectForKey:[MysectionTitles objectAtIndex:indexPath.section]]objectAtIndex:indexPath.row]objectForKey:@"id"]; if ([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark) { [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; }else{ [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; [myArray addObject:stringId]; NSLog(@"myArray %@",myArray); } }
試試這個代碼
在創建新的NSMubaleArray *storeArray;
NSMutableArray,分配storeArray = [[NSMutableArray alloc]init];
在viewDidLoad
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.section) {
case 0:
if ([storeArray containsObject:[mainArray objectAtIndex:indexPath.row]])
{
[storeArray removeObject:[mainArray objectAtIndex:indexPath.row]];
}else{
[storeArray addObject: [mainArray objectAtIndex:indexPath.row]];
}
break;
case 2:
if ([storeArray containsObject:[mainArray objectAtIndex:indexPath.row]])
{
[storeArray removeObject:[mainArray objectAtIndex:indexPath.row]];
}else{
[storeArray addObject: [mainArray objectAtIndex:indexPath.row]];
}
break;
case 3 :
if ([storeArray containsObject:[mainArray objectAtIndex:indexPath.row]])
{
[storeArray removeObject:[mainArray objectAtIndex:indexPath.row]];
}else{
[storeArray addObject: [mainArray objectAtIndex:indexPath.row]];
}
break;
default:
break;
}
NSLog(@"%@",storeArray);
}
首先為數組創建一個出口,然后在.m中進行合成
現在在viewdidLoad中初始化數組
現在實現cellforrowatIndexpath和didSelectRowAtIndexPath
查找代碼,讓我知道您是否需要澄清
String_Name = [ArrayName objectAtIndex:indexPath.row]; if ([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark) { [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; }else{ [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; [myArray addObject:String_Name]; NSLog(@"myArray %@",myArray); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.