开发者

UITableView not inserting new row

开发者 https://www.devze.com 2023-01-14 05:37 出处:网络
I am trying to update my UITableView and the following implementation is not working. I\'m wondering if I am doing something wrong?

I am trying to update my UITableView and the following implementation is not working. I'm wondering if I am doing something wrong?

NSDictionary *newContact = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:@"Name", @"Phone", nil]  forKeys: [NSArray arrayWithObjects:strName, strPhone, nil]];
[arrQuickDialContacts addObject:newContact];

NSLog(@"Returned %@ with phone %@\nNew Count: %d", strName, strPhone, [arrQuickDialContacts count]);

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:([arrQuickDialContacts count] - 1) inSection:0];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];

[objQuickDialTableView beginUpdates];
[objQuickDialTableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[objQuickDialTableView endUpdates];

Here are my delegate methods for the UITableView:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    // How many rows?   
    return [arrQuickDialContacts count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // See if we have any cells available for reuse
    UITableViewCell *objCell = [tableView dequeueReusableCellWithIdentifier:@"QuickDialCell"];
    if (objCell == nil) {

        // No reusable cell exists, so let's create a new one
        objCell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: @"QuickDialCell"];
    }

    // Give it data
    NSDictionary *objRow = [arrQuickDialContacts objec开发者_Go百科tAtIndex: [indexPath row]];
    objCell.textLabel.text = [objRow valueForKey:@"Name"];

    // Return the created cell
    return objCell;

}

And finally, my viewDidLoad populates the initial contents of the array:

- (void)viewDidLoad {

    // Load the quick dial contacts
    NSString *strPlist = [[NSBundle mainBundle] pathForResource:@"QuickDialContacts" ofType:@"plist"];
    arrQuickDialContacts = [[NSMutableArray alloc] initWithContentsOfFile: strPlist];

    [super viewDidLoad];
}

Thanks in advance!


need to call [tableView reloadData];

Acutally there is a similar question with more specifics

Unable to update UITableView

0

精彩评论

暂无评论...
验证码 换一张
取 消