ios - 自定义 tableview 单元格下的 UILabel 未更新

标签 ios objective-c uitableview

我的 tableview 单元格未在 viewDidAppear 上更新:

-(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 是一个 bool 值,已从另一个类触发:

if(self.responseAdd.responseStat.status)
        {
            self.commentTxt.text=@"";
            [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);

        }

如您所见,我已经为我的主类创建了一个对象,即 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];

    }

最佳答案

这就是您需要在 objective 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
      }

    });
});
}

这是我在 Swift

中的原始逻辑
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

                    }
                }
            }

        })

    }

关于ios - 自定义 tableview 单元格下的 UILabel 未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37043457/

相关文章:

ios - 电池安全编码

iphone - 顺时针旋转 UIView 大于 180 度的角度

ios - 只有一个 View Controller 响应方向设置

iphone - 关于JSONKit analyze的问题

ios - UITableView 不会更新数据

ios - 跨多个 subview 传递 UITouch * 的正确方法是什么?

javascript - 从 Javascript 调用 iOS Webview 委托(delegate)未触发

iOS swift imageView无法在TableViewCell中隐藏

ios - UIScrollView setContentSize 是如何工作的

ios - UITableViewCell 中的自动布局 UILabel