HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – “尝试将第0行插入第0部分,但更新后第0部分只有0行”错误大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个应用程序,从他们的联系人列表中选择一个人,并采取他们的名字,姓氏和电子邮件.然后它将一个名称保存到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将再次询问datasource的节数和行数.如果直接来自datasource的节和行的数量,则数字节和行,加上插入,减去删除必须等于numberOfSections和numberOfRowsInSection的结束调用返回的数字.

最后一个提示:避免混合调用’reloadData’和beginupdate / endupdate对.使用其中一个,而不是两个.

大佬总结

以上是大佬教程为你收集整理的ios – “尝试将第0行插入第0部分,但更新后第0部分只有0行”错误全部内容,希望文章能够帮你解决ios – “尝试将第0行插入第0部分,但更新后第0部分只有0行”错误所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。