Ios 如何在uitableview的to单独部分中显示json子值?
我尝试过多种方法,我不知道该怎么做。谁能告诉我如何实现这一点 json值:Ios 如何在uitableview的to单独部分中显示json子值?,ios,objective-c,uitableview,nsdictionary,nsindexpath,Ios,Objective C,Uitableview,Nsdictionary,Nsindexpath,我尝试过多种方法,我不知道该怎么做。谁能告诉我如何实现这一点 json值: dict=@{ @"car":@[@"hatchbag",@"sedan",@"suv"], @"waterpurifier":@[@"one litre",@"two litre",@"three litre"] }; 我的代码: subArray=[dict objectforkey:@"car"]; - (NSInteger)tab
dict=@{
@"car":@[@"hatchbag",@"sedan",@"suv"],
@"waterpurifier":@[@"one litre",@"two litre",@"three litre"]
};
我的代码:
subArray=[dict objectforkey:@"car"];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section
NSLog(@"expand section %d",expandSection);
if (expandSection == section) {
return [subArray count]+1;
}
else
return 1;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
// Configure the cell...
if (indexPath.row == 0) {
cell.textLabel.text = [arrPlayList objectAtIndex:indexPath.section];
cell.imageView.image = [UIImage imageNamed:@"twitter_icon_29x29.png"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
说明:
我已经将json值存储到单独的数组中,但我不清楚如何将这些单元格分开,如何将这些数据显示到单独的单元格中
Header:
car
->hatchbag
->sedan
->suv
waterpurifier
->0ne litre
->two litre
->three litre
如何在表视图中这样显示数据。您应该使用表视图中的部分将相关数据分组在一起 请尝试以下代码:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section
if (section==0) {
return [[dict objectForKey:@"car"] count];
}
else
{
return [[dict objectForKey:@"waterpurifier"] count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
// Configure the cell...
if (indexPath.section == 0) {
cell.textLabel.text = [[dict objectForKey:@"car"] objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:@"twitter_icon_29x29.png"];
}
else if (indexPath.section == 1)
{
cell.textLabel.text = [[dict objectForKey:@"waterpurifier"] objectAtIndex:indexPath.row];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;// otherwise use dynamic condition like this way [[dict allKey] count]; -> return number of section
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section==0) {
return [dict objectForKey:@"car"];
}
else
{
return [dict objectForKey:@"waterpurifier"];
}
}
*swift 3.1 这段代码是为swift 3.1编写的。试试看,也许会有帮助 func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{ } func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
let cell: UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: "Cell")
// Configure the cell...
if indexPath.section == 0 {
cell?.textLabel?.text = dict["car"][indexPath.row]
cell?.imageView?.image = UIImage(named: "twitter_icon_29x29.png")
}
else if indexPath.section == 1 {
cell?.textLabel?.text = dict["waterpurifier"][indexPath.row]
}
cell?.accessoryType = .disclosureIndicator
}
func numberOfSections(在tableView:UITableView中)->Int{
}
func tableView(tableView:UITableView,titleForHeaderInSection:Int)->String?{
if section == 0 {
return dict["car"]
}
else {
return dict["waterpurifier"]
}
}啊!一分钟前没有回答:)干得好。在[[dict objectforkey:@“car”]objectAtIndex:indexath.row]这个对象和indexath不是为我而来的,而我在编写objectAtIndex:indexath.row时没有来。它显示了一些错误兄弟@DharmeshDhorajiyaLet我们。
return 2
// otherwise use dynamic condition like this way [[dict allKey] count]; -> return number of section
if section == 0 {
return dict["car"]
}
else {
return dict["waterpurifier"]
}