OStack程序员社区-中国程序员成长平台

标题: ios - 如何使用objective-c用两个NSMutableArray填充可扩展的TableView [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 10:41
标题: ios - 如何使用objective-c用两个NSMutableArray填充可扩展的TableView

我正在使用 Expandable UITableview由 Tom Fewster 创建。我想使用两个 NSMutableArray 来调整示例,这是一个场景,如果有人想要从 webservice json 数据填充可扩展/折叠 TreeView 表,则需要实现。因此,由于在他的示例中 GroupCell 没有数组,我想知道我该怎么做?请记住,我的 Objective-C 仍然生锈,所以我要问这个问题。

我的尝试只显示组的第一个 ObjectAtIndex:indexPath:0。

我希望能够填充表格并获得这样的输出;

如果您以这种方式更好地理解它,您也可以使用 JSON 数据来解释您的答案。

在这里我想用 JSON 数据填充表格,以便 GroupCell 显示 class_name 和 rowCell 显示 subject_name。这是我从 JSON Web 服务解析的控制台;

(
    {
    "class_id" = 70;
    "class_name" = Kano;
    subject =         (

            "subject_id" = 159;
            "subject_name" = "Kano Class";
        }
    );
},
    {
    "alarm_cnt" = 0;

    "class_id" = 71;
    "class_name" = Lagos;
    subject =         (

            "subject_id" = 160;
            "subject_name" = "Lagos Class";
        }
    );
},
    {
    "alarm_cnt" = 3;
    "class_id" = 73;
    "class_name" = Nasarawa;
    subject =         (

            "subject_id" = 208;
            "subject_name" = "DOMA Class";
        },

            "subject_id" = 207;
            "subject_name" = "EGGON Class";
        },

            "subject_id" = 206;
            "subject_name" = "KARU Class";
        },

            "subject_id" = 209;
            "subject_name" = "LAFIA Class";
        },

            "subject_id" = 161;
            "subject_name" = "Nasarawa State Class";
        }
    );
},
    {
    "alarm_cnt" = 2;
    "class_id" = 72;
    "class_name" = Rivers;
    subject =         (

            "subject_id" = 162;
            "subject_name" = "Rivers Class";
        }
    );
}

)

我试过了,这是我的片段

- (UITableViewCell *)tableViewExpandableTableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath

{ 静态 NSString *CellIdentifier = @"RowCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

NSDictionary *d=[_sitesJson objectAtIndex:0] ;
NSArray *arr=[d valueForKey"subject_name"];
NSDictionary *subitems = [arr objectAtIndex:0];
NSLog(@"Subitems: %@", subitems);
NSString *siteName = [NSString stringWithFormat"%@",subitems];
cell.textLabel.text =siteName;
//}
NSLog(@"Row Cell: %@", cell.textLabel.text);
// just change the cells background color to indicate group separation
cell.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
cell.backgroundView.backgroundColor = [UIColor colorWithRed:232.0/255.0 green:243.0/255.0 blue:1.0 alpha:1.0];

return cell;

}

- (UITableViewCell *)tableViewExpandableTableView *)tableView cellForGroupInSectionNSUInteger)section

{ 静态 NSString *CellIdentifier = @"GroupCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *textLabel = (UILabel *)[cell viewWithTag:2];
NSDictionary *d2 = [_regionsJson objectAtIndex:0];
NSArray *arr2 = [d2 objectForKey"class_name"];
NSString *regions = [[arr2 objectAtIndex:section]objectAtIndex:0];
textLabel.textColor  = [UIColor whiteColor];
textLabel.text = [NSString stringWithFormat: @"%@ (%d)", regions, (int)[self tableView:tableView numberOfRowsInSection:section]];
NSLog(@"Group cell label: %@", textLabel.text);

// We add a custom accessory view to indicate expanded and colapsed sections
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed"ExpandableAccessoryView"] highlightedImage:[UIImage imageNamed"ExpandableAccessoryView"]];
UIView *accessoryView = cell.accessoryView;
if ([[tableView indexesForExpandedSections] containsIndex:section]) {
    accessoryView.transform = CGAffineTransformMakeRotation(M_PI);
} else {
    accessoryView.transform = CGAffineTransformMakeRotation(0);
}
return cell;

}



Best Answer-推荐答案


他,只需要一点点更新一个方法

- (UITableViewCell *)tableViewExpandableTableView *)tableView cellForGroupInSectionNSUInteger)section
{
    static NSString *CellIdentifier = @"GroupCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    NSIndexPath *indexPath;
    NSString *regions = [[_dataGroup objectAtIndex:section]objectAtIndex:0];
    cell.textLabel.text = [NSString stringWithFormat: @"%@ ", regions];

    // We add a custom accessory view to indicate expanded and colapsed sections
    cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed"ExpandableAccessoryView"] highlightedImage:[UIImage imageNamed"ExpandableAccessoryView"]];
    UIView *accessoryView = cell.accessoryView;
    if ([[tableView indexesForExpandedSections] containsIndex:section]) {
        accessoryView.transform = CGAffineTransformMakeRotation(M_PI);
    } else {
        accessoryView.transform = CGAffineTransformMakeRotation(0);
    }
    return cell; 
}

可以帮到你。

HTH,享受编码!

关于ios - 如何使用objective-c用两个NSMutableArray填充可扩展的TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29667542/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4