【问题标题】:Scrolling issue Table View with JSON data带有 JSON 数据的滚动问题表视图
【发布时间】:2014-01-04 22:53:03
【问题描述】:

我正在将一些数据从位于我的服务器上的 JSON 文件解析到我的表视图。 当我启动应用程序时,应用程序成功地将数据下载到我的表格视图中,但是当我开始滚动时,应用程序崩溃了。

这是我的代码:

#import "FirstViewController.h"
#import "YoutubePost.h"
#import "AFNetworking.h"

@interface FirstViewController ()

@end

@implementation FirstViewController
@synthesize tableView = _tableView, activityIndicatorView = _activityIndicatorView, movies = _movies;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Videos", @"Videos");
        self.tabBarItem.image = [UIImage imageNamed:@"newtab1"];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self.navigationController setNavigationBarHidden:YES];

    self.tableView.separatorColor = [UIColor clearColor];

    // Setting Up Activity Indicator View
    self.activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    self.activityIndicatorView.hidesWhenStopped = YES;
    self.activityIndicatorView.center = self.view.center;
    [self.view addSubview:self.activityIndicatorView];
    [self.activityIndicatorView startAnimating];
    self.tableView.separatorColor = [UIColor clearColor];

    // Initializing Data Source
    self.movies = [[NSArray alloc] init];

    NSURL *url = [[NSURL alloc] initWithString:@"http://my-website.com/link-to-json.php?name=Name&orderby=published"];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        self.movies = JSON;
        [self.activityIndicatorView stopAnimating];
        [self.tableView reloadData];

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
    }];

    [operation start];
}

// Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.movies && self.movies.count) {
        return self.movies.count;
    } else {
        return 0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 378;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *simpleTableIdentifier = @"YoutubePost";

    YoutubePost *cell = (YoutubePost *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"YoutubePost" owner:self options:nil];
        cell = [nib objectAtIndex:0];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }

    NSDictionary *movie = [self.movies objectAtIndex:indexPath.row];
    cell.title.text = [movie objectForKey:@"title"];

    NSURL *url = [[NSURL alloc] initWithString:[movie objectForKey:@"link"]];

    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];    
    NSString * storyLink = [[_movies objectAtIndex: storyIndex] objectForKey:@"link"];

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:storyLink]];
    NSString *formattedJSON = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:[self.tweets objectAtIndex:indexPath.row] options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding];
    NSLog(@"tweet:\n%@", formattedJSON);
}

@end

怎么了?我看到数据成功下载到我的自定义表格视图单元格,但每次我尝试向下滚动应用程序时都会崩溃。请帮我解决这个问题。

谢谢。

【问题讨论】:

  • 应用崩溃时控制台会显示什么?
  • 你能发布你的崩溃日志吗?为什么会崩溃?
  • @NickGalasso 崩溃日志完全为空。
  • 您是否将 YoutubePost 单元格的 nib 文件中的自定义单元格 Identifier 设置为“YoutubePost”?
  • @Refael.S 是的。我不认为这是问题所在,因为当我启动应用程序时,一切都正确解析并且我的自定义单元格工作正常。但是当我开始滚动应用程序时冻结。

标签: ios json parsing uitableview


【解决方案1】:

更正一下:

    // Initializing Data Source
    //self.movies = [[NSArray alloc] init];

    NSURL *url = [[NSURL alloc] initWithString:@"http://my-website.com/link-to-json.php?name=Name&orderby=published"];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        self.movies = [[NSArray alloc] initWithArray:JSON];
        [self.activityIndicatorView stopAnimating];
        [self.tableView reloadData];

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
    }];

    [operation start];

并在您的 YouTubePost Nib 中添加标识符“YouTubePost”:

并且还在您的 YouTubePost Nib 中选择您的标题 UILabel 并在检查器中更改:

收件人:

或改变:

收件人:

这样就可以了。

【讨论】:

    【解决方案2】:

    更改以下行:

    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"YoutubePost" owner:self options:nil];
        cell = [nib objectAtIndex:0];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    }
    

    像这样:

    if (cell == nil)
    {
         UINib *nib = [UINib nibWithNibName:@"YoutubePost" bundle:nil];
    
        [tableView registerNib:nib forCellReuseIdentifier:@"YoutubePost"];
    
        tableViewCell = [tableView dequeueReusableCellWithIdentifier:@"YoutubePost"];
    }
    

    【讨论】:

    • 能否请您提供更详细的信息?我们需要知道你的数组中存储了什么。除此之外,YouToubePost 是什么?它是从 UITableViewCell 子类化的吗?有没有同名的笔尖?
    【解决方案3】:

    您似乎没有在 JSON 中对 [NSNull null] 进行任何检查。以下代码可能会给您[NSNull null],而不是NSDictionary:

    NSDictionary *movie = [self.movies objectAtIndex:indexPath.row];
    

    您应该在调用reloadData之前过滤掉这些

    同样,此行也可能返回[NSNull null]:

    cell.title.text = [movie objectForKey:@"title"];
    

    您需要准备好处理这种情况。这可能会对您有所帮助:
    Replace all NSNull objects in an NSDictionary

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-25
      • 1970-01-01
      • 1970-01-01
      • 2020-07-21
      • 1970-01-01
      • 2011-02-26
      • 1970-01-01
      相关资源
      最近更新 更多