【问题标题】:how to load in table cell? the following is json data如何在表格单元格中加载?以下是json数据
【发布时间】:2016-04-16 18:26:33
【问题描述】:
[{
    "firstName": "John",
    "lastName": "Doe"
}, {
    "firstName": "Anna",
    "lastName": "Smith"
}, {
    "firstName": "Peter",
    "lastName": "Jones"
}]

我有这些类型的 json 数据,如何在表格单元格中加载,加载名字和姓氏的键值是什么?

【问题讨论】:

  • 是 [0]["firstName"] 吗?
  • 你能展示一些你迄今为止尝试过的代码吗?

标签: ios json swift uitableview


【解决方案1】:

斯威夫特版本

 //create a struct to represent your data
 struct People {
     var firstName: String!
     var lastName: String!
 }

 //create an array to store your data in your class
 var peopleArray: [People]?

 //when the view loads, take the data you receive and parse it
 for person in dataReceived {
     let personToAdd = People(firstName: person["firstName"], lastName: person["lastName"]
     peopleArray.append(personToAdd)
 }

 //cell for row at index path
 let cell = tableView.dequeueReusableCellWithIdentifier("identifier")
 let personForCell = peopleArray[indexPath.row]
 cell.textLabel!.text = personForCell.firstName
 cell.detailTextLabel.text = personForCell.lastName

【讨论】:

    【解决方案2】:
    @property (nonatomic, strong) NSArray *responseArray;
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // get JSON response from server or local to NSArray
        _responseArray = [NSJSONSerialization JSONObjectWithData:yourJsonHere options:0 error:nil];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellIdentifier"];
    
        NSDictionary *row = _responseArray[indexPath.row];
        [cell.textLabel setText:row[@"firstName"]];
        [cell.detailTextLabel setText:row[@"lastName"]];
        return cell;
    }
    

    相应地更改您的UITableViewCellStyle 或为它们创建您自己的标签。

    【讨论】:

    • 原始问题中没有显示代码(因此被否决) - 但问题包含swift 标签。这对我来说看起来并不快。
    • 我不在乎,我仍然会尽我所能帮助那个人。如果他知道答案或认识开发人员,他就不会在这里。我的回答总比没有回答好..
    猜你喜欢
    • 1970-01-01
    • 2015-10-29
    • 1970-01-01
    • 2014-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-29
    • 2015-06-08
    相关资源
    最近更新 更多