How to populate UITableView with plist

为君一笑 提交于 2019-11-28 10:47:45

问题


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:


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.



来源:https://stackoverflow.com/questions/8133812/how-to-populate-uitableview-with-plist

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