【问题标题】:Obj-C/iOS - Adding rows to a dynamic uitableviewObj-C/iOS - 将行添加到动态 uitableview
【发布时间】:2015-06-23 02:29:26
【问题描述】:

所以,我正在创建一个应用程序,它有一个包含联系人的表格视图。用户从他们的联系人列表中添加联系人。

问题是;我可以添加人员,但直到重新启动后才会出现。

我已经搜索了其他问题/答案,但到目前为止没有任何帮助。

这是我的加人操作按钮:

 - (IBAction)addPerson:(id)sender {
ABPeoplePickerNavigationController* picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
[self presentViewController:picker animated:YES completion:nil];
}

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
[self dismissViewControllerAnimated:YES completion:nil];
}

-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{
[self dismissViewControllerAnimated:YES completion:nil];
}

我使用了 2 个可变数组,一个用于存储名称,另一个用于存储数字。

这里是导航控制器:

-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{

NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];

numbers = [[NSMutableArray alloc] initWithArray:[defaults objectForKey:@"numbers"]];
people =  [[NSMutableArray alloc]  initWithArray:[defaults objectForKey:@"people"]];

if(people.count == 0 && numbers.count == 0){
    numbers = [[NSMutableArray alloc] init];
    people =  [[NSMutableArray alloc] init];
}

这是表格应该自我更新的地方,但它不会。

NSString *contactName = CFBridgingRelease(ABRecordCopyCompositeName(person));
NSString* nameField = [NSString stringWithFormat:@"%@", contactName ? contactName : @"No Name"];
ABMultiValueRef phoneRecord = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFStringRef phoneNumber = ABMultiValueCopyValueAtIndex(phoneRecord, 0);
NSString* phoneField = (__bridge_transfer NSString *)phoneNumber;
CFRelease(phoneRecord);

**[tableview beginUpdates];**
[people addObject:nameField];
[numbers addObject:phoneField];

[defaults setObject:people forKey:@"people"];;
[defaults setObject:numbers forKey:@"numbers"];
[self dismissViewControllerAnimated:YES completion:nil];

 //[tableview insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
 //since it's a dynamic table, I'm not sure if this works.
**[tableview endUpdates];**
return NO;
}

我是否错过了其他一些 tableview 更新? 如果是这样,那应该去哪里?

我们将不胜感激任何形式的帮助。 如果我在没有 **'ed 行(更新)的情况下运行代码,它会成功添加但需要重新启动才能出现。 如果我使用带有 ** 的行或使用 beginUpdates 运行代码,我会得到 NSInternalInconsistencyException,第 0 节中的行数无效。

顺便说一句,tableview 和 tableView 是两个不同的 UITableView。

【问题讨论】:

    标签: ios objective-c xcode uitableview nsarray


    【解决方案1】:

    您还应该更新 numberOfRowsInSection 中的新行数。

    [people addObject:nameField];
    [numbers addObject:phoneField];
    [defaults setObject:people forKey:@"people"];;
    [defaults setObject:numbers forKey:@"numbers"];
    [self dismissViewControllerAnimated:YES completion:nil];
    
    [tableview beginUpdates];
    
    [tableview insertRowsAtIndexPaths:(int)people.count withRowAnimation:UITableViewRowAnimationFade];
    
    [tableview endUpdates];
    
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        //if (section == n) {
        //    return ...
        //}
        return (int)people.count;        
    }
    

    【讨论】:

    • 我也想过这个问题。但我不知道我该怎么做。
    • 在 [tableview insertRowsAtIndexPaths:people.count withRowAnimation:UITableViewRowAnimationFade];我得到“ARC不允许将'nsuinteger'(又名'unsigned int')隐式转换为'NSArray *'
    • 我不确定如何添加 (int) 作为答案。我将那部分代码更改为 [tableview insertRowsAtIndexPaths: (int) people.count withRowAnimation:UITableViewRowAnimationFade];我得到的只是'不兼容的整数到指针转换将'int'发送到'NSArray *'类型的参数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多