简体   繁体   中英

Linking UITableView with a NSMutableArray

I've been trying to fill a UITableView with data from a NSMutableArray. I have a View-Based application project and so far I've tried every tutorial I found but so far, it just doesn't work. I linked the dataSource and delegate to the File's Owner, I added the procotols UITableViewDelegate and UITableViewDataSource to my UIViewController, I create an array containing 3 strings just to try and debug, everytime I load the app, the table stays empty.

I'm not sure what could be my error, or where I should look to find the problem, if you guys got any idea that would be nice.

Thank you

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

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }


    NSUInteger row = [indexPath row];

    cell.textLabel.text = [yourArray objectAtIndex:row];

    return cell;
}

That should populate the table from an array. I've used it several times in my app.

Make sure the dataSource and delegate are properly connected to the view controller, also make sure your UITableView is property connected with the outlet if you are using storyboards or a xib file.

If nothing works I recommend you to use XLData that makes UITableView data loading much more simple to implement.

我想您确实忘记了在表视图上调用-reloadData

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM