2013-03-25 80 views
0

我閱讀了很多關於我的問題的帖子,但都沒有工作過,所以在此先感謝您的幫助。ios reloadData只重新加載不可見的單元格

我有一個tableview,加載一個自定義的筆尖。我的數據來自網絡JSON文件。我已經實現了一個刷新調用(即時覆蓋這只是一個單元格)。

調用該方法後,將調用並分析新的JSON。細胞更新。我可以通過滾動tableview來確認這一點,我可以看到我的新行。但是可見的行仍然存在。

我試圖使數組零,循環和刪除基於對象,但似乎沒有任何工作。

這裏是代碼

#import "MMNewsViewController.h" 
#import "MMCatsDetailController.h" 
#import "CellView.h" 
#import "FeatureCell.h" 
//#import "MMnewsData.h" 



@interface MMNewsViewController() 

@end 

@implementation MMNewsViewController 
@synthesize titleData; 
@synthesize imageData; 
@synthesize imageLargeData; 
@synthesize dateData; 
@synthesize descriptionData; 
@synthesize linkData; 
@synthesize cityData; 

@synthesize _jsonResult; 
@synthesize _jsonResultImage; 
@synthesize _jsonResultImageLarge; 
@synthesize _jsonResultDate; 
@synthesize _jsonResultDescription; 
@synthesize _jsonResultLink; 
@synthesize _jsonResultCity; 
@synthesize featureImage; 
@synthesize region; 





- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UINib *nib = [UINib nibWithNibName:@"CellView" bundle:nil]; 
    [self.tableView registerNib:nib forCellReuseIdentifier:@"CellView"]; 

    [self.navigationController.navigationBar setTintColor:[UIColor blackColor]]; 
    self.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:0]; 

    self.navigationController.title = @"News"; 

    self.tabBarItem.title = @"FooBar"; 
    self.title = @"Music News"; 



UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init]; 

refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to Refresh"]; 

[refreshControl addTarget:self action:@selector(refreshView:)   
forControlEvents:UIControlEventValueChanged]; 

[self.view addSubview:refreshControl]; 
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES]; 

    region = @"wellington"; 
    [self makeDataRequests]; 


} 

-(void)makeDataRequests 
{ 


    NSString *strURL = [NSString stringWithFormat:@"MYSITEjson.php?region=%@",region ]; 



    NSMutableData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL] ]; 
    NSMutableDictionary *arrayResult = [NSJSONSerialization JSONObjectWithData:dataURL options:kNilOptions error:nil]; 

    NSMutableArray *dataArray = [arrayResult objectForKey:@"tickets"]; //2 

    _jsonResult = [[NSMutableArray alloc] init]; 
    _jsonResultImage = [[NSMutableArray alloc] init]; 
    _jsonResultImageLarge = [[NSMutableArray alloc] init]; 
    _jsonResultDate= [[NSMutableArray alloc] init]; 
    _jsonResultDescription= [[NSMutableArray alloc] init]; 
    _jsonResultLink= [[NSMutableArray alloc] init]; 
    _jsonResultCity= [[NSMutableArray alloc] init]; 


    for(NSMutableDictionary *newsDict in dataArray){ 
     NSMutableDictionary *titleDict = [newsDict objectForKey:@"title"]; 
     NSMutableDictionary *imageDict = [newsDict objectForKey:@"image"]; 
     NSMutableDictionary *imageLargeDict = [newsDict objectForKey:@"imageLarge"]; 
     NSMutableDictionary *dateDict = [newsDict objectForKey:@"pubDate"]; 
     NSMutableDictionary *descriptionDict = [newsDict objectForKey:@"description"]; 
     NSMutableDictionary *linkDict = [newsDict objectForKey:@"link"]; 

     NSMutableDictionary *cityDict = [[newsDict objectForKey:@"venue"] objectForKey:@"city"]; 


     [_jsonResult addObject:titleDict]; 
     [_jsonResultImage addObject:imageDict]; 
     [_jsonResultImageLarge addObject:imageLargeDict]; 
     [_jsonResultDate addObject:dateDict]; 
     [_jsonResultDescription addObject:descriptionDict]; 
     [_jsonResultLink addObject:linkDict]; 
     [_jsonResultCity addObject:cityDict]; 
     // NSLog(@"%@",cityDict); 

    } 

    // NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding]; 



    self.titleData = _jsonResult; 
    self.imageData = _jsonResultImage; 
    self.imageLargeData = _jsonResultImageLarge; 
    self.dateData = _jsonResultDate; 
    self.descriptionData = _jsonResultDescription; 
    self.linkData = _jsonResultLink; 
    self.cityData = _jsonResultCity; 


[self performSelector:@selector(delayedReloadData) withObject:nil afterDelay:1.5]; 
} 
-(void) delayedReloadData 
{ 

    NSLog(@"delayedReloadData"); 
    [self.tableView reloadData]; 

} 

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation{ 

     NSLog(@"reloadRowsAtIndexPaths"); 
} 


- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table view data source 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 


    NSLog(@"COUNTER: %i",[self.titleData count]); 
    // Return the number of rows in the section. 
    return [self.titleData count]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     NSLog(@"cellForRowAtIndexPath"); 

    NSUInteger row = [indexPath row]; 



     static NSString *CellIdentifier = @"CellView"; 

     CellView *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if (cell == nil){ 
      NSLog(@"New Cell Made"); 

      NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CellView" owner:nil options:nil]; 

      for(id currentObject in topLevelObjects) 
      { 
       if([currentObject isKindOfClass:[CellView class]]) 
       { 
        cell = (CellView *)currentObject; 
        break; 
       } 
      } 
     } 



     [[cell titleLabel] setText:[titleData objectAtIndex:row]]; 
     [[cell dateLabel] setText:[cityData objectAtIndex:row]]; 

     NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString:[imageData objectAtIndex:row]]]; 
     UIImage *img1 = [UIImage imageWithData:data]; 
     [[cell imageLabel] setImage:img1]; 

     return cell; 


    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 


} 


#pragma mark - Table view delegate 
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated{} 



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Navigation logic may go here. Create and push another view controller. 

    MMCatsDetailController *detailViewController = [[MMCatsDetailController alloc] initWithNibName:@"MMCatsDetailController" bundle:nil]; 



    NSUInteger row = [indexPath row]; 
    NSString *titleSender = [titleData objectAtIndex:row]; 
    NSString *dateSender = [dateData objectAtIndex:row]; 
    NSString *imageSender = [imageData objectAtIndex:row]; 
    NSString *imageLargeSender = [imageLargeData objectAtIndex:row]; 
    NSString *descriptionSender = [descriptionData objectAtIndex:row]; 
    NSString *linkSender = [linkData objectAtIndex:row]; 

    detailViewController.titleSend = titleSender; 
    detailViewController.dateSend = dateSender; 
    detailViewController.imageSend = imageSender; 
    detailViewController.imageLargeSend = imageLargeSender; 
    detailViewController.descriptionSend = descriptionSender; 
    detailViewController.linkSend = linkSender; 

     //NSLog(@"linkSender=%@",linkSender); 
     region = @"nelson"; 
[self makeDataRequests]; 
    // [self.navigationController pushViewController:detailViewController animated:YES]; 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

} 
-(void)refreshView:(UIRefreshControl *)refresh { 
     refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Refreshing data..."]; 
     // custom refresh logic would be placed here... 
     region = @"nelson"; 
    [self makeDataRequests]; 


    NSLog(@"HEre"); 

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateFormat:@"MMM d, h:mm a"]; 
     NSString *lastUpdated = [NSString stringWithFormat:@"Last updated on %@", 

             [formatter stringFromDate:[NSDate date]]]; 
    refresh.attributedTitle = [[NSAttributedString alloc] initWithString:lastUpdated]; 

     [refresh endRefreshing]; 
    NSLog(@"HEre"); 

} 

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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSUInteger row = [indexPath row]; 

    if(row==0){ 
    //  return 200; 
    } 
    else{ 


// return 96; 

      } 
     return 96; 
} 


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 


    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,tableView.frame.size.width,10)]; 

    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, headerView.frame.size.width, headerView.frame.size.height)]; 

    headerLabel.font = [UIFont boldSystemFontOfSize:12]; 
    headerLabel.text = @"Music News"; 
    headerLabel.backgroundColor = [UIColor blackColor]; 
    headerLabel.textColor = [UIColor whiteColor]; 
    [headerView addSubview:headerLabel]; 

    return headerView; 

} 


@end 
+0

嗨,感謝您的意見和建議。我讀了更多關於CGD和主線程的內容。 主要問題是在XIB上 - 我有一個文件所有者爲視圖和它包含的表設置。 ARGH - 哦現在排序很好 – user2206088 2013-03-25 05:27:23

+0

對不起,但我有點不明白這個問題。你能解釋一下你的意思嗎?「但是可見的行仍然存在。」? – Kunal 2013-03-25 06:18:31

回答

0

所有你需要做的是對的tableView呼叫reloaddata。或者你沒有在你認爲正在調用的tableView上調用reloaddata,你的單元格沒有在tableView:cellForRowAtIndexPath:中正確設置,或者你正在調用reloaddata,而不是在主線程中。

您不應該從UI線程對服務器進行同步調用。瞭解一下GCD。在對服務進行非阻塞呼叫時,dispatch_async函數將派上用場。

+1

看完你的代碼後。我想你可能在一個單獨的線程上調用reloaddata。試試這樣做:dispatch_async(dispatch_get_main_queue(),^ {[self.tableView reloadData;}]; – 2013-03-25 03:55:00

+0

嗨,感謝您的意見。選擇(delayedReloadData) withObject:無 waitUntilDone:NO]; 他們似乎都做同樣的事情,他們兩個呼叫的cellForRowAtIndexPath,numberOfRowsInSection,numberOfSectionsInTableView一兩件事,這並不被調用是reloadRowsAtIndexPaths – user2206088 2013-03-25 04:52:03

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

CueTableCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

if (cell == nil) { 
    NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"CueTableCell XibName" owner:self options:nil]; 
    // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain). 
    cell = [array objectAtIndex:0]; 
} 

return cell; 
} 
+0

超出了略有不同的風格。加載筆尖這有什麼不同? – 2013-03-25 04:31:35

相關問題