开发者

What is the easiest way to populate a uitableview with a huge list of data?

开发者 https://www.devze.com 2023-04-04 04:17 出处:网络
I\'ve made a uitab开发者_运维技巧leview before but with minimal cells. I was wondering how to populate the table with 100 plus names, and each have and individual detail view according to their name.

I've made a uitab开发者_运维技巧leview before but with minimal cells. I was wondering how to populate the table with 100 plus names, and each have and individual detail view according to their name. Also, searchable. Thank you.

Note: this data is copy and pasted from a website. I currently have it in an excel document.


The easiest is to make a plist file from the data with the following NSArray and NSDictionary format:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
  <dict>
    <key>Name 1</key>
    <string>Description 1</string>
  </dict>
  <dict>
    <key>Name 2</key>
    <string>Description 2</string>
  </dict>
</array>
</plist>

Add this file to your application. When you want to access it, you do this by

NSString *path=[[NSBundle mainBundle] pathForResource:@"MyList" ofType:@"plist"];
NSArray *array =[NSArray arrayWithContentsOfFile:path];OfFile:path];

Now you have an NSArray containing NSDictionaries, each of which has the information you need for your UITableView.

All you need to do now is to populate the UITableView using this array. Do this by implementing these two methods:

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

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // Create your cells
    NSDictionary *dict = [array objectAtIndex:indexPath.row];
    // use the key and description however you like in your cell
}
0

精彩评论

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