gpt4 book ai didi

ios - "Attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update"错误

转载 作者:技术小花猫 更新时间:2023-10-29 10:41:55 30 4
gpt4 key购买 nike

我有一个应用程序可以从他们的联系人列表中选择一个人,并获取他们的名字、姓氏和电子邮件。然后它将名字保存到 nsmutablearray 并将其放入 uitableview 单元格中。在模拟器中选择联系人后,我的问题就出现了。

代码:

.h:

#import <UIKit/UIKit.h>
#import <AddressBookUI/AddressBookUI.h>

@interface FirstViewController : UIViewController < ABPeoplePickerNavigationControllerDelegate, UITableViewDelegate, UITableViewDataSource>

- (IBAction)showPicker:(id)sender;

@property (weak, nonatomic) IBOutlet NSString *firstName;

@property (weak, nonatomic) IBOutlet NSString *email;

@property (weak, nonatomic) IBOutlet NSString *lastName;


@property (weak, nonatomic) IBOutlet UITableView *myTableView;

@property (strong, nonatomic) NSMutableArray *contacts;

@end

.m:

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

@synthesize firstName;

@synthesize email;

@synthesize lastName;

@synthesize contacts;

@synthesize myTableView;


- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
contacts = [[NSMutableArray alloc]initWithObjects:nil];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - UITableView Datasource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return contacts.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

}

cell.textLabel.text = [contacts objectAtIndex:indexPath.row];

return cell;
}

- (IBAction)showPicker:(id)sender {

ABPeoplePickerNavigationController *picker =
[[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;

[self presentModalViewController:picker animated:YES];
}

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


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

[self displayPerson:person];
[self dismissModalViewControllerAnimated:YES];

return NO;
}

- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier
{





return NO;

}


- (void)displayPerson:(ABRecordRef)person
{
NSString* name = (__bridge_transfer NSString*)ABRecordCopyValue(person,
kABPersonFirstNameProperty);
self.firstName = name;

NSString* last = (__bridge_transfer NSString*)ABRecordCopyValue(person,
kABPersonLastNameProperty);
self.lastName = last;


ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
NSString *emailId = (__bridge NSString *)ABMultiValueCopyValueAtIndex(emails, 0);//0 for "Home Email" and 1 for "Work Email".

self.email = emailId;

if (!(contacts))
{

contacts = [[NSMutableArray alloc]init];

}

[contacts insertObject:firstName atIndex:0];

NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

[self.myTableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}




@end

最佳答案

UITableView 必须始终与数据源保持同步。如果数据源可以在后台线程中更改,则必须特别小心。

当有东西被添加到数据源时,尽快调用beginUpdate/insert/endUpdate。您不必担心缓存这些,UITableView 会在确定有足够的 CPU 时间和资源时缓存要执行的更改。

调用 endUpdates 时,UITable 将再次向数据源询问部分和行数。如果您的节数和行数直接来自数据源,那么节数和行数加上插入减去删除必须等于结束调用 numberOfSections 和 numberOfRowsInSection 返回的数字。

最后一个提示:避免混合调用“reloadData”和 beginUpdate/endUpdate 对。使用其中之一,而不是两者。

关于ios - "Attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17005635/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com