How to populate UITableView with plist

2019-01-29 01:26发布

I have a simple UITableView that I'd like to populate using a plist file. I have followed a few tutorials but they all seem to over complicate the process and in the end I end up getting errors or my app simply does not work.

I'm using a window based application.

Cheers, Sam

1条回答
看我几分像从前
2楼-- · 2019-01-29 01:43

To load the file :

thearray = [NSArray arrayWithContentsOfFile:thePath];

Then you need to set your controller as the data source and delegate for the table view and implement at least :

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

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

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell* cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];

    // assuming your array contains only simple strings :
    cell.textLabel.text = [thearray objectAtIndex:indexPath.row];

    return cell;
}

This doesn't use the "reuse" of cells which is important if your list is big.

查看更多
登录 后发表回答