[英]How to add data dynamically to UITableView when tapped on section headers
[英]UITableView - get data when tapped
我的问题是,当单元被点击时,是否有可能获取文本数据并在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;
}
我认为您的代码有误。 通过检查来检查json中是否有任何数据
NSLog(@"%@ and %@",[info objectForKey:@"Artist"], [info objectForKey:@"Song"]);
告诉我是否有结果 如果没有,那么您的php中就有一些错误,否则您的数据应该显示在那里。
您编写的代码与您一开始所解释的矛盾。 但是,这是应该怎么做的:
首先,您需要获取要显示的数据。 您可以从外部网站(如您所说的php),plist,xml或任何您想要的文件中获取。 您可以在启动表视图时 (例如使用ASIHTTPRequest )或在启动表视图并仅使用自定义init方法将其发送之前执行此操作。 其次,您需要阅读您收到的内容。 通常,很棒的是使用NSArray格式。 这取决于您要从中获取数据的数据源的实现。
如果您不熟悉Objective-C和可可接触,则应该从一些有关UITableView的快速教程开始。 我自己写过一个书,但是目前我猜它(非常)已经过时了。 您仍然可以查看一下: http : //paulpeelen.com/2009/08/14/developing-with-iphone-3-0-x-simple-uitableview/
使用此代码方法。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//retrive your data here.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.