0

我目前正在开发一个应用程序,我只想加载包含 media_url 的推文。我曾尝试阻止它将推文加载到 tableView 中,但是由于明显的原因,这会导致大量空白推文。

我用于此的代码:

- (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];
    }
    id tweet = [self.timeline objectAtIndex:[indexPath row]];

    NSString *example = [NSString stringWithFormat:@"%@",[tweet valueForKeyPath:@"entities.media.media_url"]];
    NSLog(@"%@", example);

    if ([example isEqual: @"(null)"] ) {

    } else {
        cell.textLabel.text = [tweet valueForKeyPath:@"text"];
        cell.detailTextLabel.text = [tweet valueForKeyPath:@"user.name"];
    }

    return cell;
}

所以现在我正在寻找一种替代方法,它只会成功加载具有 media_url 的推文,并将它们按出现的顺序排列。我正在使用 NSJSONSerialization 来解析我的数据。

我用于此的代码:

- (void)fetchData
{
    [_refreshHeaderView refreshLastUpdatedDate];
    NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1/statuses/home_timeline.json?count=199&include_entities=true"];
    TWRequest *request = [[TWRequest alloc] initWithURL:url 
                                                 parameters:nil 
                                              requestMethod:TWRequestMethodGET];
    [request setAccount:self.account];    
    [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
        if ([urlResponse statusCode] == 200) {
            NSError *jsonError = nil;
            id jsonResult = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonError];
            if (jsonResult != nil) {
                self.timeline = jsonResult;
                dispatch_sync(dispatch_get_main_queue(), ^{
                    [self.tableView reloadData];
                });                
            }
            else {
                NSString *message = [NSString stringWithFormat:@"Could not parse your timeline: %@", [jsonError localizedDescription]];
                [[[UIAlertView alloc] initWithTitle:@"Error" 
                                            message:message
                                           delegate:nil 
                                  cancelButtonTitle:@"Cancel" 
                                  otherButtonTitles:nil] show];
            }
        }
    }];
   [self performSelector:@selector(doneLoadingTableViewData) withObject:nil afterDelay:1.0];  
}

所以我想知道无论如何我可以将数据添加到时间轴中,只有那些包含 media_url 的推文,或者是否有更好的方法使用不同的方法来做到这一点。

谢谢。

4

1 回答 1

1

也许尝试使用这样的方法来检查推文是否包含媒体网址:

if([tweet valueForKey:@"entities.media.media_url"] == [NSNull null])
于 2013-05-27T15:01:55.230 回答