希望你不会介意,如果我为你写一些代码。不要只是复制和粘贴它。试着理解它的方式。以便您将来可以解决此类需求。
我有你的数据plist
并将其提取到NSMutableDictionary
然后NSMutableArray
像这样。
// In the viewDidLoad: method
NSMutableDictionary *plistData = [NSMutableDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"Countries" ofType:@"plist"]];
sectionedArray = [plistData objectForKey:@"Country"];
那么下面的tableView
方法。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [sectionedArray count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSMutableArray *array = [[sectionedArray objectAtIndex:section] objectForKey:@"citys"];
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];;
}
// Configure the cell...
NSMutableArray *array = [[sectionedArray objectAtIndex:indexPath.section] objectForKey:@"citys"];
NSDictionary *dict = [array objectAtIndex:indexPath.row];
cell.textLabel.text = [dict valueForKey:@"cityName"];
return cell;
}
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
NSMutableArray *array = [[sectionedArray objectAtIndex:indexPath.section] objectForKey:@"citys"];
[array removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
NSLog(@"Sectioned array %@", sectionedArray);
}
}