2016-04-16 29 views
-2
[{ 
    "firstName": "John", 
    "lastName": "Doe" 
}, { 
    "firstName": "Anna", 
    "lastName": "Smith" 
}, { 
    "firstName": "Peter", 
    "lastName": "Jones" 
}] 

私はこれらのタイプのjsonデータを持っていますが、どのようにテーブルセルにロードできますか?firstnameとlastnameを読み込むためのキー値は何でしょうか?テーブルセルに読み込む方法は?以下はjsonデータです

+0

のですか? – Ranch

+0

これまでに試したことのあるコードを表示できますか? – ventiseis

答えて

2

スウィフトバージョン

//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 
0
@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を適宜変更するか、独自のラベルを作成してください。それは[ "firstNameの"] [0]

+0

元の質問にはコードが表示されていなかったため投票されましたが、質問には 'swift'タグが含まれていました。これは私には素早く見えません。 – ventiseis

+1

私は気にしない、私はまだ私ができる人を助けるでしょう。彼が答えを知っていた、または開発者を知っていれば、彼はここにいなかったでしょう。私の答えは無回答より良いです.. – emotality

関連する問題