我一直在試圖弄清楚這一點,而不是提出一個解決方案。我有一個視圖控制器與表和第一個單元格表分配給一個名爲「添加朋友」的按鈕。點擊後,它會將您帶到另一個視圖控制器,並在表中包含聯繫人列表。當你點擊一個人時,它會返回到另一個視圖控制器並添加所選人員。這是我迄今爲止所擁有的。從一個視圖控制器添加對象到另一個視圖控制器上的陣列
ContactsViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
FirstViewController *newVC = [self.storyboard instantiateViewControllerWithIdentifier:@"newVCSegue"];
newVC.peopleArray = [[NSMutableArray alloc] init];
Person *user = [contactsList objectAtIndex:indexPath.row];
NSArray *userKeys = [NSArray arrayWithObjects:@"FirstName", @"LastName", nil];
NSArray *userObjects = [NSArray arrayWithObjects:user.firstName, user.lastName, nil];
NSDictionary *userDictionary = [NSDictionary dictionaryWithObjects:userObjects forKeys:userKeys];
[newVC.peopleArray addObject:userDictionary];
[self.navigationController pushViewController:newVC animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
FirstViewController.h
@property (strong, nonatomic) NSMutableArray *peopleArray;
FirstViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//...
if (indexPath.row == 0) {
contactName.text = @"Add Person";
imgView.image = [UIImage imageNamed:@"plus-icon.png"];
} else {
NSString *firstName = [[peopleArray objectAtIndex:(indexPath.row)-1] objectForKey:@"firstName"];
NSString *lastName = [[peopleArray objectAtIndex:(indexPath.row)-1] objectForKey:@"lastName"];
contactName.text = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
}
return cell;
}
這讓我再補充一個朋友,到目前爲止,如果我決定到另一個增加該列表取代了添加的第一位朋友。
後在做什麼你合成peopleArray?如果沒有,綜合它,並使用它作爲self.peopleArray而不是peopleArray ... – 2013-05-06 04:40:06