2016-05-05 41 views
0

的tableview細胞是沒有得到關於viewDidAppear :的UILabel沒有得到更新

-(void)viewDidAppear:(BOOL)animated 
{ 

    [[ApiAccess getSharedInstance] setDelegate:self]; 
    [[[SocektAccess getSharedInstance]getSocket]setDelegate:self]; 
    [[[SocektAccess getSharedInstance]getSocket] reconnect]; 
    _chatSocket =[[SocektAccess getSharedInstance]getSocket]; 


    if(self.updateWill) 
    { 
     NSLog(@"viewDidAppear"); 
     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.updateId inSection:0]; 

     NSLog(@"Update Value: %d",self.updateValue); 
     NSLog(@"Update ID: %d",self.updateId); 



     TimelineTableViewCell *cell = (TimelineTableViewCell *)[self.tableData cellForRowAtIndexPath:indexPath]; 


     WallPost *data = self.myObject[indexPath.row]; 
     data.commentCount = self.updateValue; 

     [self.myObject replaceObjectAtIndex:indexPath.row withObject:data]; 

     WallPost *data2 = self.myObject[indexPath.row]; 

     NSLog(@": %d",data2.commentCount); 


     cell.commentLabel.text = @" "; 



     [self.tableData beginUpdates]; 
     [self.tableData reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone]; 
     [self.tableData reloadData]; 
     [self.tableData endUpdates]; 

    } 

    self.updateWill = NO; 
} 

self.updateWill更新是一個布爾值,而且已經從另一個類觸發:

if(self.responseAdd.responseStat.status) 
     { 
      [email protected]""; 
      [self getData]; 
      TimelineViewController *t = (TimelineViewController *)self.navigationController.viewControllers[0]; 
      t.updateWill = YES; 
      t.updateId = self.index; 
      t.updateValue =(int)self.response.responseData.count+1; 
      NSLog(@"response %d",(int)self.response.responseData.count); 

     } 

正如你所看到的,我已經創建了我的Main類的一個對象,它是TimelineViewController並添加了updateWill和其他值需要的屬性。但細胞沒有重新加載! w 帽子我在這裏做錯了?

更新

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


    TimelineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
    self.counter = 0; 

    WallPost *data = self.myObject[indexPath.row]; 

    cell.image.userInteractionEnabled = YES; 
    cell.image.tag = indexPath.row; 

    if(self.updateWill == YES) 
    { 
     NSLog(@"YES YES"); 
     data.commentCount= (int)self.updateValue; 



     [self.tableData beginUpdates]; 
     [self.tableData reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone]; 
     [self.tableData reloadData]; 
     [self.tableData endUpdates]; 
     cell.commentLabel.text = [NSString stringWithFormat:@"%d comments",data.commentCount]; 


    } 
    else 
    { 
     data.commentCount = (int)data.comments.count; 
     cell.commentLabel.text = [NSString stringWithFormat:@"%d comments",data.commentCount]; 

    } 
+0

其實,事情是,你需要做的更新tableview的cellforrowindex方法內的東西委託,而不是視圖的生命週期方法。所以,確保你也一樣。 – Himanshu

+0

我試過,但它沒有工作! @Himanshu – Fay007

+0

更新你的cellforrowindex代碼可能有問題。 – Himanshu

回答

2

這是你需要在目標C

-(void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 

[self.tableData reloadData]; 

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){ 
    //Background Thread 
    dispatch_async(dispatch_get_main_queue(), ^(void){ 
     //Run UI Updates 
    NSArray *paths = [self.tableData indexPathsForVisibleRows]; 
    for (NSIndexPath *path in paths) 
    { 
      //get desired cell here 
      CustomCell *cell = (CustomCell *)[(UITableView *)self.view cellForRowAtIndexPath: path 
]; 

     cell.labeView //now you can update here on your cell items 
     } 

    }); 
}); 
} 

要解決什麼,這是我在原來的邏輯斯威夫特

override func viewDidAppear(animated: Bool) 
    { 
     super.viewDidAppear(animated) 


     tableView.reloadData() 
     dispatch_async(dispatch_get_main_queue(), {() -> Void in 

      //get visible cells indexpaths 
      if let indices = self.tableView.indexPathsForVisibleRows 
      { 
       for index in indices 
       { 
        if index.section == 0 //this is specific to me, checking if section 0 is visible 
        { 
         //get desired cell here 
         let cell = self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 0,inSection: 0)) as! CustomCell 

         cell.myImageView.userInteractionEnabled = true //now i can update imageview on table cell 

        } 
       } 
      } 

     }) 

    } 
+0

但它不適合我!什麼都沒發生 !!! – Fay007

+0

根據我的代碼調整它後,它實際上工作:) – Fay007

+0

@ Fay007我很高興它可以幫助你;) – swiftBoy

0

在viewDidLoad中

[self.tableData reloadData]; 

我希望這將是有益的。

+0

爲什麼需要viewDidLoad – Fay007