ios - 重新加载 UItableview 未更新标签

标签 ios objective-c uitableview uilabel

我有一个 UITableview,我用来自 Web 服务的值填充 UITableview。我的 UILABEL 被正确填充了正确的数据。

我的问题是,当我尝试更新 UILabel 的颜色时没有任何反应?

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

    static NSString *CellIdentifier = @"nil";

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:nil];

    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleSubtitle
                reuseIdentifier:CellIdentifier] ;
    }

    self.tableView.separatorInset = UIEdgeInsetsZero;

    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    // Sets the Label for the cell
    self.hungryPersonOrderStatusLabel = [[UILabel alloc] init];
    self.hungryPersonOrderStatusLabel.frame = CGRectMake(80, 30, 150, 25);
    self.hungryPersonOrderStatusLabel.backgroundColor = [UIColor clearColor];
    self.hungryPersonOrderStatusLabel.textAlignment = NSTextAlignmentJustified;
    self.hungryPersonOrderStatusLabel.font = [UIFont fontWithName:@"SegoeWP" size:13.0];
    self.hungryPersonOrderStatusLabel.textAlignment = NSLineBreakByTruncatingTail;
    self.hungryPersonOrderStatusLabel.numberOfLines = 1;
    self.hungryPersonOrderStatusLabel.text = [[self.viewOrderUserArray valueForKey:@"OrderStatus"] objectAtIndex:[indexPath row]];
    self.hungryPersonOrderStatusLabel.tag = 1001;
    self.hungryPersonOrderStatusLabel.textColor = [UIColor appGreenColor];
    [cell.contentView addSubview:self.hungryPersonOrderStatusLabel];
}

-(void)getOrderDetails 
{

    [self showSpinner];

    DeliNinjaWebService *webService = [DeliNinjaWebService alloc];

    [webService viewOrderDetails:self.deli.deliId  success:^(NSArray *response) 
    {

        NSLog(@"Response Object %@",response );

        [self hideSpinner];

        self.viewOrderDetailsArray = [response mutableCopy];
        self.viewOrderUserArray = [[response valueForKeyPath:@"Orders"] mutableCopy];

        NSString *orderStatusString = [[self.viewOrderUserArray valueForKey:@"OrderStatus"] componentsJoinedByString:@""];

        if ([orderStatusString isEqualToString:@"In Progress"]) 
        {

            self.hungryPersonOrderStatusLabel.textColor = [UIColor appRedColor];
            [self.tableView reloadData];
        }

        [self.tableView reloadData];

    } failure:^(NSString *error) 
    {
        [self hideSpinner];

        NSLog(@"Fail %@", error);

    } noConnection:^
    {
        [self hideSpinner];

        [self showMessage:@"No Connection Error" Message:@"Please check your internet connection"];
    }];
}

我尝试在此处添加标签,然后添加一个 subview ,但没有发生任何事情,tableview 没有更新。

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

    static NSString *CellIdentifier = @"nil";

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:nil];
    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        self.hungryPersonOrderStatusLabel = [[UILabel alloc] init];
        self.hungryPersonOrderStatusLabel.frame = CGRectMake(80, 30, 150, 25);
        self.hungryPersonOrderStatusLabel.backgroundColor = [UIColor clearColor];
        self.hungryPersonOrderStatusLabel.textAlignment = NSTextAlignmentJustified;
        self.hungryPersonOrderStatusLabel.font = [UIFont fontWithName:@"SegoeWP" size:13.0];
        self.hungryPersonOrderStatusLabel.textAlignment = NSLineBreakByTruncatingTail;
        self.hungryPersonOrderStatusLabel.numberOfLines = 1;
        self.hungryPersonOrderStatusLabel.tag = 1001;
        self.hungryPersonOrderStatusLabel.textColor = [UIColor appGreenColor];
        [self.hungryPersonOrderStatusLabel setTag:999];
    }

    UILabel *label = (UILabel *)[cell.contentView viewWithTag:999];
    label.text = [[self.viewOrderUserArray valueForKey:@"OrderStatus"] objectAtIndex:[indexPath row]];

}

enter image description here

最佳答案

这是一个简单的错误:-

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

  static NSString *CellIdentifier = @"nil";
  UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:nil];
  ......
  self.hungryPersonOrderStatusLabel.textColor = [UIColor appGreenColor];
}

 -(void)getOrderDetails {
 [self showSpinner];

 if ([orderStatusString isEqualToString:@"In Progress"]) {
    self.hungryPersonOrderStatusLabel.textColor = [UIColor appRedColor];
    [self.tableView reloadData];
  }
  ......
}

首先,在 cellForRowAtIndexPath 方法中将标签颜色设置为绿色,然后在 getOrderDetails 方法中将标签颜色设置为红色,但是当您重新加载方法时,它再次只采用绿色。所以最后不会有颜色变化。

所以你应该使用一个 bool 标志,即 statusFlag(最初它是假的)所以代码将是(在 cellForRowAtIndexPath 方法中)

 if(statusFlag)
    self.hungryPersonOrderStatusLabel.textColor = [UIColor appGreenColor];
 else
    self.hungryPersonOrderStatusLabel.textColor = [UIColor appRedColor];

同样在你的 cellForRowAtIndexPath 方法中,

self.hungryPersonOrderStatusLabel.backgroundColor = [UIColor clearColor];   //Which is clear and not changing it at any place.

因此您可以使用 if 条件和标志以相同的方式更改它。

关于ios - 重新加载 UItableview 未更新标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25660910/

相关文章:

iphone - 当我从 Xib 文件加载自定义 UITableViewCells 时发生泄漏?

ios - Stripe 确认 Swift 卡的详细信息

ios - 在 PayPal 中启用信用卡付款会导致模拟器和物理设备崩溃

ios - iOS Google Maps SDK 中具有跨宽度渐变的折线

iphone - UITableViewCell 在取消选择时选择了阴影颜色

iphone - 如何删除 UITableView 的边框?

ios - 我可以以编程方式检查 UITableViewCell 是什么样式吗?

iphone - TableView 下方的 UIRefreshControl

ios - 像在 Java 中一样在每个字段上获取和设置

ios - 在 SwiftUI 中监听 AVPlayer timeControlStatus 的变化