【问题标题】:"Attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update" Error“尝试将第 0 行插入第 0 节,但更新后第 0 节中只有 0 行”错误
【发布时间】:2013-06-09 01:47:05
【问题描述】:

我有一个应用程序从他们的联系人列表中选择一个人,并获取他们的名字、姓氏和电子邮件。然后它将名字保存到 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

【问题讨论】:

  • numberOfRowsInSection 中的固定数字 4 看起来很可疑。不应该是[contacts count]吗?
  • 你的联系人不应该成为表格视图的数据源吗?为什么行数被硬编码为 4?
  • @MartinR 该应用程序还有另一个目的,因此出于其他原因将数量限制为 4
  • @DanGrueneberg:但是如果您调用insertRowsAtIndexPaths(带有一个索引路径),那么表格视图预计 numberOfRowsInSection 会增加一。
  • @MartinR:我对代码进行了更改,但仍然出现相同的错误。

标签: ios objective-c uitableview addressbook


【解决方案1】:

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

向数据源添加内容后,请尽快调用 beginUpdate/insert/endUpdate。您不必担心缓存这些,UITableView 会在确定有足够的 cpu 时间和资源时缓存要执行的更改。

在调用 endUpdates 的那一刻,UITable 将再次向 dataSource 询问节数和行数。如果您的节数和行数直接来自数据源,则节数和行数加上插入、减去删除必须等于对 numberOfSections 和 numberOfRowsInSection 的最终调用返回的数字。

最后一个提示:避免混合调用“reloadData”和 beginUpdate/endUpdate 对。使用其中一种,不要同时使用。

【讨论】:

    【解决方案2】:

    我遇到了同样的问题。你所要做的就是改变

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

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

    来自UITableView 文档

    beginUpdates

    Begin a series of method calls that insert, delete, or select rows and sections of the receiver.

    当你使用beginUpdates时,你必须调用endUpdates而不是reloadData

    您可以查看this link 了解更多UITableView 信息。

    【讨论】:

    • 这对我不起作用,我很快。也使用了开始更新和结束更新,但速度很快。
    【解决方案3】:

    上面关于实现-tableView:numberOfRowsInSection: 的cmets 是正确的。断言正在检查返回的值,期望它会增加。

    但是,由于您没有在 UITableView 上设置您的 dataSource,因此它调用(或不调用)一个不存在的方法并获取 0

    您需要将myTableView.dataSource 和(因为您还实现了委托协议)myTableView.delegate 设置为self

    你也可能需要类似的东西

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
        ...
    }
    

    除非您在其他地方注册,或者您的故事板有一个“原型单元”,其标识符为 "Cell",这是您的代码要求的。

    【讨论】:

      【解决方案4】:

      我发现当我在视图控制器中放置表格视图时,通常会出现此问题。如果你使用的是 UITableViewController 跳转到 3.

      这些步骤可能会有所帮助:

      1:确保在您的 View Controller .h 文件中添加以下内容:

      @interface YourViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
      

      2:接下来通过 ctrl + 拖动到您的 .h 类为您的表格视图创建一个 IBOutlet。它应该看起来像:

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

      3:下一步是 ctrl + 拖动到您的视图控制器图标(见图)

      您需要这样做两次选择:

      - delegate
      - datasource
      

      最后,在你的 .m 文件中,你应该有以下方法:

      - (void) viewWillAppear:(BOOL)animated{
          //[self.tableView beginUpdates];
          //[self.tableView endUpdates];
      
          [self.tableView reloadData];
      }
      

      您可以使用 beginUpDates/endUpdates 或 reloadData,但 Apple 文档建议使用 reloadData。

      完成后,您的桌子应该可以正常工作了。

      【讨论】:

        【解决方案5】:

        您需要保持联系人数组的计数相应地增加。
        在创建 indexPath 时,您需要设置适当的 indexPathForRow: 和节数(如果需要)。

        - (void)displayPerson:(ABRecordRef)person
        {
        
          ..........
        
        [contacts insertObject:firstName atIndex:0];
        
        NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; // you cant do this increment appropriately
        
         .........
        }
        

        【讨论】:

          【解决方案6】:

          请检查您的 tableview 数据源和委托方法。您可能会将空数组传递给数据源方法。并且不要忘记在获取数据后重新加载tableview。

          【讨论】:

            【解决方案7】:

            我知道这很愚蠢 - 我得到了同样的错误,因为我忘记设置委托和数据源。

            所以在插入行并执行tableView.endUpdates() 之后,tableView 认为它必须有一些行 - 但由于未链接的dataSource,它没有。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-12-10
              • 1970-01-01
              • 1970-01-01
              • 2020-10-11
              相关资源
              最近更新 更多