Refreshing XML data and updating a UITableView

南笙酒味 提交于 2019-12-04 21:15:28

You should only be setting your data source once and it should not be an NSArray. The data container from which you pull your records can change, but the data source should always be the same. In most cases the data source should just be your view controller. Then your view controller should implement the methods in the UITableViewDataSource protocol including:

– tableView:cellForRowAtIndexPath:  required method
– numberOfSectionsInTableView:
– tableView:numberOfRowsInSection:  required method
– sectionIndexTitlesForTableView:
– tableView:sectionForSectionIndexTitle:atIndex:
– tableView:titleForHeaderInSection:
– tableView:titleForFooterInSection:

An NSArray doesn't respond to any of these methods which is why you are getting the unrecognized selector message. You have to implement them yourself.

You should familiarize yourself with the Table View Programming Guide to understand how to effectively use table views.

Best regards.

UPDATE: Here's a little code that might help you. In your RootViewController instantiate the NSArray with alloc/init in the -viewDidLoad. Call it items:

- (void)viewDidLoad;
{
    [super viewDidLoad];
    items = [[NSArray alloc] init];
}

Then implement your table view's data source delegates like this:

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    return [items count];
}

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

Then you need to implement your cellForRowAtIndexPath:

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

    cell = [tv dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"] autorelease];

    }

    // Get your item from the items array here
    NSDictionary *item = [items objectAtIndex:[indexPath row]];
    NSString *text = [item valueForKey:@"text"];

    [[cell textLabel] setText:text];

    return cell;

}

This code assumes that the objects in your NSArray are NSDictionaries. If you are using some custom object, cast the object at that index path to your custom type and then use it's fields.

When you have new data available and need to reload your table view, you will simply re-allocate your items NSArray and then call reloadData on the tableview. Say you have a reload occurring like this:

- (void)didReciveNewData:(NSArray*)newItems;
{
    if (items) [items release], items = nil;
    items = [newItems copy];
    [tableView reloadData];
}

This will cause the table view to query its view controller for the number of rows to display and the cells for each row again which are provided by accessing the count and contents of the NSArray.

HTH.

I'm guessing your data is loading asynchronously in your getData method (if you aren't you should be), which is updating/invalidating the reference to the datasource when the ui least expects it, causing crashes when the ui attempts to render with a stale pointer to its data.

Just make sure you never modify the contents of the tablecontrollersdata from a different thread.

or maybe you're trying to use coredata, in which case, i have no idea.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!