【问题标题】:IPhone TableView not Populating JSON DataiPhone TableView 不填充 JSON 数据
【发布时间】:2011-12-28 00:50:10
【问题描述】:

我有一个页面,其中包含一个 tableview - 以及一个将邮政编码发送到 PHP 页面并返回 JSON 数据的文本字段和按钮。我无法用返回的数据填充表格视图。 NSLog 显示正在返回数据,但该表仍为空白。没有错误。 代码:

- (IBAction) searchzip: (id) sender
{
NSString *post = [NSString stringWithFormat:@"zip=%@", zipField.text];
NSString *hostString =     @"https://www.mysite.com/searchzip.php?";

// Append string and add percent escapes
hostString = [[hostString stringByAppendingString:post] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *hostURL = [NSURL URLWithString:hostString];
NSString *jsonString = [[NSString alloc] initWithContentsOfURL:hostURL];
self.zipArray = [jsonString JSONValue]; 
NSLog(@"%@", zipArray);
[jsonString release];
}

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


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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
NSDictionary *infoDictionary = [self.zipArray objectAtIndex:indexPath.row];
static NSString *Prospects = @"agencyname";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Prospects];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:Prospects]   autorelease];
}

// setting the text
cell.text = [infoDictionary objectForKey:@"agencyname"];    
self.navigationItem.title = @"Zip Search";

// Set up the cell
return cell;

}

头文件:

@interface SearchZipViewController : UITableViewController{
NSMutableArray *zipArray;
IBOutlet UITextField *zipField;
IBOutlet UIButton *searchButton;
}
@property (nonatomic, retain) NSMutableArray *zipArray;
@property (nonatomic, retain) UITextField *zipField;
@property (nonatomic, retain) UIButton *searchButton;
- (IBAction) searchzip: (id) sender;
@end

【问题讨论】:

    标签: objective-c cocoa-touch iphone-sdk-3.0


    【解决方案1】:

    我相信基本问题是您正在尝试构建复合视图(文本字段、按钮和表格视图)并使用 UITableViewController 作为控制器。在 SO 和其他地方已经引用了这基本上是不可行的。相反,请尝试以下操作:

    @interface SearchZipViewController : UIViewController<UITableViewDelegate, UITableViewDataSource> {
        IBOutlet UIButton* searchButton;
        IBOutlet UITextField* zipField;
        IBOutlet UITableView* tableView;
    }
    
    @end
    

    然后将 IB 中的 UITableView 连接到 File's Owner 中的 tableView ivar - 从您的 cmets 看来,您正试图将复合视图中的表视图元素连接到 File's Owner,@ 987654325@——不是你想要的。

    【讨论】:

    • 好的,我通过在我的标题中添加一个 IBOutlet 来修复 tableView。但是还是没有数据。我将 tableView 指向表格视图。我应该把 [tableView reloadData] 放在哪里;就像声明的那样。我仍然没有显示任何数据。
    • 好吧,正如 sosborn 所说,我会把它放在 searchzip 方法的末尾。我会在那里放一个断点,然后在每个 tableview 委托方法中也放一个。确保在调用 reloadData 方法后触发 TV 委托方法。特别是,逐步执行cellForRowAtIndexPath 方法以确保您实际上拥有给定行的有效数据。或者,此时您至少应该在该方法中包含 NSLog 语句,以确保您确实拥有您认为拥有的数据。
    • 另外,您似乎在使用SBJSON?您确定您有一个对象数组——即“[{},{},{}]”不是别的吗?测量两次,切割一次:)
    • 是的,正在运行 NSLog(cell);没有返回任何东西......我有一个 NSLog(@"%@", zipArray);它会正确返回 JSON,但它是 ({}{}) 格式
    • 好的——如果你在cellForRowAtIndexPath 中放置一个断点,你初始化变量infoDictionary 它的值是多少?它是一个有效的对象吗?
    【解决方案2】:

    在搜索 zip 方法的末尾,您应该添加以下行:

    [tableView reloadData];
    

    否则您的 tableView 无法知道您有新数据要显示。

    【讨论】:

    • 嗯,我做到了,但仍然没有。在我的编辑器上,我有 UITableView 的数据源和指向 Files Owner 的委托,对吗?
    • 是的,应该没错。当视图加载时,cellForRow 是否真的被调用? [infoDictionary objectForKey:@"agencyname"] 是否返回您期望的结果?
    • 是的,它正确获取数据。这与我的编辑器设置有关。我将视图设置为表格视图,但带有一个文本字段和一个按钮,底部有一个表格视图。在文件所有者下,我有指向 TableView 的视图。在自动将 JSON 加载到表视图中的其他页面上,我将 tableView 指向了表视图。如果我在此页面上执行此操作,它会删除文本字段和按钮....
    • 基本上我认为的问题是我实际填充数据的tableView 没有显示在我的文件所有者的出口上。我只是不知道如何解决它。
    • 所以你在 UITableViewController 中添加了一个 tableView?是的,如果你这样做,你可能会遇到问题。尝试从 UIViewController 而不是 UITableViewController 继承。
    【解决方案3】:

    您应该使用cell.textLabel.text 来设置表格单元格的文本。 text 属性已弃用。

    另外,请尝试在该行设置断点,以确保您在那里的局部变量实际上具有您期望的值(即正确的 JSON 值)。也许您的某个关键字符串(如大小写)有错字?

    【讨论】:

      猜你喜欢
      • 2011-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-24
      相关资源
      最近更新 更多