0

我不断收到此错误:

2013-03-30 19:48:40.029 try[14838:907] -[__NSSetM objectAtIndex:]:无法识别的选择器发送到实例 0x1e8ea660 2013-03-30 19:48:40.030 try[14838:907] * 由于应用程序终止uncaught exception 'NSInvalidArgumentException', reason: '-[__NSSetM objectAtIndex:]: unrecognized selector sent to instance 0x1e8ea660' * First throw call stack: (0x33e523e7 0x3bb43963 0x33e55f31 0x33e5464d 0x33dac208 0xe5bc7 0x35cac569 0x35c91391 0x35ca8827 0x35c648c7 0x35a10513 0x35a100b5 0x35a10fd9 0x35a109c3 0x35a107d5 0x35a10639 0x33e27941 0x33e25c39 0x33e25f93 0x33d9923d 0x33d990c9 0x3797733b 0x35cb52b9 0x7b495 0x3bf70b20) libc++abi.dylib:终止调用抛出异常 (lldb)

// 这是我的代码:

- (void)fetchTweets
{

    UIApplication* app = [UIApplication sharedApplication];
    app.networkActivityIndicatorVisible = YES;


    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSData* data = [NSData dataWithContentsOfURL:
                        [NSURL URLWithString: @"***.json"]];

        NSError* error;



        tweets = [NSJSONSerialization JSONObjectWithData:data
                                                 options:kNilOptions
                                                   error:&error];

        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    });
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return tweets.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"showVideo";

    CustomVideoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(!cell)
    {
        cell = [[CustomVideoCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 277, 58)];
    av.backgroundColor = [UIColor clearColor];
    av.opaque = NO;
    av.image = [UIImage imageNamed:@"cell_bg.png"];
    cell.backgroundView = av;


    UIImage *image = [UIImage   imageNamed:@"disclosure_arrow_white.png"];
    UIImageView *av2 = [[UIImageView alloc] initWithFrame:CGRectMake(44.0, 44.0, image.size.width, image.size.height)];
    av2.backgroundColor = [UIColor clearColor];
    av2.opaque = NO;
    av2.image = [UIImage imageNamed:@"disclosure_arrow_white.png"];
    cell.accessoryView = av2;



    NSDictionary *tweet = [tweets objectAtIndex:indexPath.row];


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


    NSLog(@"t: %@",[tweet objectForKey:@"title"]);


    return cell;

}
4

2 回答 2

1

You can try retaining tweets viz.

tweets = [[NSJSONSerialization JSONObjectWithData:data
                                                 options:kNilOptions
                                                   error:&error] retain];

and later release it in dealloc. I guess its an auto released object getting assigned to it.

于 2013-04-01T19:16:34.603 回答
0

行后:

CustomVideoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

您需要检查该单元格实际上是否具有值,如果没有则创建一个新单元格。

CustomVideoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
  cell = [[CustomVideoCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:Cellidentifier];
}

祝你好运!

更新您的新错误:

JSONObjectWithData 产生的“tweets”变量是 NSDictionary,而不是 NSArray。由于 NSDictionary 没有实现 'objectAtIndex',所以会抛出无法识别的选择器异常。通常,推文字典应该有一个包含推文数组的特定键,您应该将其分配给 fetchTweets 方法中的 'tweets' 变量。

If you're not sure about a lot of the stuff I am referring to, then I strongly recommend you use the Sensible TableView framework. The framework will be able to automatically fetch all the web service tweets for you and automatically display them on the table view. I personally always use it now, saves me a ton of manual labor.

于 2013-03-30T23:29:53.380 回答