我是iOS开发的新手。经过多次尝试;有很多示例代码,我设法从我的服务器解析一个json字符串,并能够在动态tableview中显示结果。我的问题是我无法使单元格可单击,因此他们会将id和标签传递给另一个视图,在该视图中将执行另一个json解析以显示该行的详细信息。
以下是我的代码:
#import "jsonviewcontroller.h"
#import "CJSONDeserializer.h"
#import "Otel_ItemViewController.h"
@implementation jsonviewcontroller
@synthesize tableview;
@synthesize rows;
- (void)dealloc {
[rows release];
[tableview release];
[super dealloc];
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [rows count];
}
// Customize the appearance of table view cells.
- (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] autorelease];
}
// Configure the cell.
NSDictionary *dict = [rows objectAtIndex: indexPath.row];
cell.textLabel.text = [dict objectForKey:@"C_NAME"];
cell.detailTextLabel.text = [dict objectForKey:@"CAT_ID"];
return cell;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:@"http://zskript.net/categories.php"];
NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url];
NSLog(jsonreturn); // Look at the console and you can see what the restults are
NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];
NSError *error = nil;
// In "real" code you should surround this with try and catch
NSDictionary * dict = [[[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error] retain];
if (dict)
{
rows = [dict objectForKey:@"users"];
}
NSLog(@"Array: %@",rows);
[jsonreturn release];
}
// Do some customisation of our new view when a table item has been selected
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure we're referring to the correct segue
if ([[segue identifier] isEqualToString:@"ShowSelectedMovie"]) {
// Get reference to the destination view controller
Otel_ItemViewController *vc = [segue destinationViewController];
// get the selected index
NSInteger selectedIndex = [[self.tableview indexPathForSelectedRow] row];
// Pass the name and index of our film
[vc setSelectedItem:[NSString stringWithFormat:@"%@", [rows objectAtIndex:selectedIndex]]];
[vc setSelectedIndex:selectedIndex];
}
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
@end
以下是将显示详细信息的视图:
#import "Otel_ItemViewController.h"
@implementation Otel_ItemViewController
@synthesize selectedIndex, selectedItem;
- (void)viewDidLoad
{
[super viewDidLoad];
[outputLabel setText:selectedItem];
[outputText setText:selectedItem];
[outputImage setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", selectedIndex]]];
}
@end
目前,当我单击表格中的单元格时,虽然我已将其设置为推送到下一个视图,但没有任何反应。有人请指教吗?
这是更新的代码。
我的根视图控制器:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dict = [rows objectAtIndex: indexPath.row];
DetailViewController *controller = [[DetailViewController alloc] init];
controller.CATNAME = [dict objectForKey:@"C_NAME"];
controller.CATNUMBER = [dict objectForKey:@"CAT_ID"];
[self.navigationController pushViewController:controller animated:YES];
[controller release];
}
这是DetailViewController.h:
@interface DetailViewController : UIViewController {
NSString *CATNAME;
NSInteger CATNUMBER;
IBOutlet UILabel *labelId;
IBOutlet UILabel *LabelName;
}
@property (nonatomic) NSInteger CATNUMBER;
@property (nonatomic, retain) NSString *CATNAME;
@end
和DetailViewController.m:
#import "DetailViewController.h"
@implementation DetailViewController
@synthesize CATNAME, CATNUMBER;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void) viewDidLoad
{
LabelName.Text = CATNAME;
labelId = CATNUMBER;
// [LabelName setText:CATNAME];
// [labelId setText:CATNUMBER];
}
答案 0 :(得分:2)
您必须实施此方法
编辑:加载新视图
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dict = [rows objectAtIndex: indexPath.row];
MyNewViewController *controller = [[MyNewViewController alloc] init];
controller.C_NAME = [dict objectForKey:@"C_NAME"];
controller.CAT_ID = [dict objectForKey:@"CAT_ID"];
[self.navigationController pushViewController:controller animated:YES];
[controller release];
}
在上面的代码中,我假设您正在使用导航控制器(这是您想要做的事情的最简单方法)。另外我假设你有一个继承自UIViewController
的类,你想要显示它。我还假设我在我的示例中调用MyNewViewController
的这个类有两个成员和属性,分别称为C_NAME和CAT_ID。
- (void) viewDidLoad
{
labelName.Text = C_NAME;
labelId = CAT_ID
}
上面我的错误,因为我正在记忆中。但是主体保持不变,如果你正确地传递变量它应该可以工作,你可以查看我的blog它仍然需要做一些工作,但它有一个很好的初学者帖子并显示如何编辑标签的文字。在上面的代码中,我假设您的视图包含两个标签labelName和labelId。
在那里,您可以访问选择的单元格,然后您可以定义需要执行的操作。