0

我的问题是,当点击单元格时,是否可以从 php 获取文本数据并在我的 ViewController 中读取它?

我猜didSelectRowAtIndexPath是这样做的关键,但我不知道如何写。

我能够从 php 获取数据到我TableViewController的如下;

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [json count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    NSDictionary *info = [json objectAtIndex:indexPath.row];
    cell.textLabel.text = [info objectForKey:@"Artist"];
    cell.detailTextLabel.text = [info objectForKey:@"Song"];


    return  cell;
}
4

3 回答 3

0

使用这种代码方法。

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ 
  //retrive your data here.
}
于 2012-05-03T11:17:29.773 回答
0

您编写的代码与您在开始时解释的内容有些矛盾。但是,应该这样做:

首先,您需要获取要显示的数据。您可以从外部网站(如您所说的 php)、plist、xml 或任何您想要的网站获取此信息。您可以在启动 tableview 时(例如使用ASIHTTPRequest)或在启动 tableview 并使用自定义 init 方法将其发送之前执行此操作。其次,您需要阅读您收到的内容。通常最好是采用 NSArray 格式。这取决于您从中获取数据的数据源的实现。

如果你是 Objective-c 和 cocoa-touch 的新手,你应该从一些关于 UITableView 的快速教程开始。我自己写过一个,但目前我猜它(非常)过时了。你仍然可以看看它:http: //paulpeelen.com/2009/08/14/developing-with-iphone-3-0-x-simple-uitableview/

于 2012-05-02T13:35:09.057 回答
0

你的代码我认为是错误的。通过检查检查json中是否有任何数据

NSLog(@"%@ and %@",[info objectForKey:@"Artist"],  [info objectForKey:@"Song"]);

并告诉我是否有任何结果出来。如果不是,那么您的 php 中有一些错误,否则您的数据应该显示在那里。

于 2012-05-02T13:32:49.420 回答