码:
.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行”错误所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)