2013-05-27 33 views
0

我目前正在開發一款應用,而我只想加載包含media_url的推文。我試圖阻止它將tweets加載到tableView中,但是由於顯而易見的原因導致大量空白的tweets。我使用該僅使用media_url檢索推文

代碼:

- (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,或是否有更好的辦法鳴叫用不同的方法做這件事。

謝謝。

回答

1

也許有這樣的嘗試,以檢查是否鳴叫包含或不是媒體網址:

if([tweet valueForKey:@"entities.media.media_url"] == [NSNull null])