如何从同一.plist填充多个表视图节

时间:2012-12-07 12:49:42

标签: objective-c ios uitableview

很抱歉,如果已经讨论过这个问题,我找不到我的意思..

我有一个.plist文件,其中包含2个数组,这些数据完全按照我希望在表格视图中拆分的部分进行拆分。

将数组放入表视图中没有问题,但是我无法理解如何告诉应用程序我想在第一部分中使用一个数组而在第二部分中需要第二个数组。

我目前将代码放入表中的代码是这样的(它可以正常工作):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Create string with reference to the prototype cell created in storyboard
    static NSString *CellIdentifier = @"PlayerNameCell";

    //Create table view cell using the correct cell type
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier      forIndexPath:indexPath];

    //Create a dictionary containing the player names
    NSDictionary *players = (NSDictionary*) [[self Squad] objectAtIndex:[indexPath row]];

    //Set the cell text to the player name
    [[cell textLabel] setText:(NSString*)[players valueForKey:@"PlayerFullName"]];

    //Set the cell detail text to the squad number
    [[cell detailTextLabel] setText:(NSString*)[players valueForKey:@"PlayerSquadNumber"]];

    return cell;
}

但现在我有另一个表视图,我需要2个部分,每个部分从不同的数组中读取。

非常感谢任何帮助。

非常感谢

2 个答案:

答案 0 :(得分:0)

好的,所以你在顶层有2个数组,每个数组都包含数组,对吗?

您需要调整几种方法。返回表视图中节数的顶级数组的数量。在cellForRowAtIndexPath:中,返回正确的部分/行的正确对象。像

这样的东西

[[sectionsArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]

答案 1 :(得分:0)

请执行以下操作:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"PlayerNameCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

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

    if( indexPath.section == 0 ){
        NSDictionary *players = (NSDictionary*) [self.array1 objectAtIndex:indexPath.row];
        cell.textLabel.text = (NSString*)[players valueForKey:@"PlayerFullName"];
        cell.detailTextLabel.text = (NSString*)[players valueForKey:@"PlayerSquadNumber"];
    } else {
        NSDictionary *players = (NSDictionary*) [self.array2 objectAtIndex:indexPath.row];
        cell.textLabel.text = (NSString*)[players valueForKey:@"PlayerFullName"];
        cell.detailTextLabel.text = (NSString*)[players valueForKey:@"PlayerSquadNumber"];
    }

    return cell;
}

将节数设置为2.对于每个节,使用[self.array2 objectAtIndex:indexPath.row]获取不同的值。

我不知道你是如何将plist保存到2个数组中的,但如果你需要帮助,请告诉我。

相关问题