简体   繁体   English

UITableView-在点击时获取数据

[英]UITableView - get data when tapped

My question is that is it possible to get text data and read it in my ViewController from php when the cell is tapped? 我的问题是,当单元被点击时,是否有可能获取文本数据并在PHP的ViewController中读取它?

I'm guessing didSelectRowAtIndexPath is the key to do this, but I'm not sure how to write. 我猜测didSelectRowAtIndexPath是执行此操作的关键,但是我不确定如何编写。

I am able to get data from php into my TableViewController as below; 我能够从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;
}

Your code is I think wrong. 我认为您的代码有误。 Check if there any data in json by checking 通过检查来检查json中是否有任何数据

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

and tell me if any result comes out. 告诉我是否有结果 if not then there is some mistake in your php else your data should show there. 如果没有,那么您的php中就有一些错误,否则您的数据应该显示在那里。

The code you have written kind of contradicts what you explained in the beginning. 您编写的代码与您一开始所解释的矛盾。 However, This is how it should be done: 但是,这是应该怎么做的:

First off you will need to get the data you want to display. 首先,您需要获取要显示的数据。 You can get this from an external website (php, as you call it), plist, xml or whatever you want. 您可以从外部网站(如您所说的php),plist,xml或任何您想要的文件中获取。 You can do this when you have your tableview initiated (eg using ASIHTTPRequest ) or before you initiate your tableview and just send it in using a custom init method. 您可以在启动表视图时 (例如使用ASIHTTPRequest )或在启动表视图并仅使用自定义init方法将其发送之前执行此操作。 Second, you need to read the contents you have recieved. 其次,您需要阅读您收到的内容。 Usually it great to get is in a NSArray format. 通常,很棒的是使用NSArray格式。 This depends on your implementation of the data source you are fetching data from. 这取决于您要从中获取数据的数据源的实现。

If you are new to objective-c and cocoa-touch, you should start off with some quick tutorials regarding UITableView. 如果您不熟悉Objective-C和可可接触,则应该从一些有关UITableView的快速教程开始。 I have written one myself, but currently I guess its (very) outdated. 我自己写过一个书,但是目前我猜它(非常)已经过时了。 You could still have a look at it though: http://paulpeelen.com/2009/08/14/developing-with-iphone-3-0-x-simple-uitableview/ 您仍然可以查看一下: http : //paulpeelen.com/2009/08/14/developing-with-iphone-3-0-x-simple-uitableview/

use this method of code. 使用此代码方法。

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ 
  //retrive your data here.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM