简体   繁体   中英

Reload UItableview is not updating label

I have a UITableview and I populate the UITableview with values from a web service. My UILABEL get correctly populated with the correct data.

My problem is that when I try to update th ecolor of the UILabel nothing happens?

- (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"];
    }];
}

and I tried to add the label here then add a subview but nothing happened the tableview did not update.

 - (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]];

}

在此处输入图片说明

It's simple mistake as:-

- (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];
  }
  ......
}

Firstly, in cellForRowAtIndexPath method your setting label color green then later in getOrderDetails method your setting your label color red but as you reload method then it again take green color only. So at end no color change will be there.

So you should take a bool flag namely, statusFlag (initially it will be false) so code will be (in cellForRowAtIndexPath method)

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

Also in ur cellForRowAtIndexPath method,

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

So you could change it same way with if condition and flag.

try this ..

after colour changing of label you are reloading the table view so the colour of label again set to what you have specified in cellForRowAtIndexPath method.so remove the line

self.hungryPersonOrderStatusLabel.backgroundColor = [UIColor clearColor]; in `cellForRowAtIndexPath` method

place that line in viewDidload or viewWillAppear methods

you forgot to set [cell.contentView addSubview:self.hungryPersonOrderStatusLabel]; in your second part

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];
    [cell.contentView addSubview:self.hungryPersonOrderStatusLabel]; // <------
}

otherway your

UILabel *label = (UILabel *)[cell.contentView viewWithTag:999];

will find no label ;)

and... why using

self.hungryPersonOrderStatusLabel.tag = 1001;

and

[self.hungryPersonOrderStatusLabel setTag:999];

? ;)

also don't need to call [self.tableView reloadData]; in the if-statement, because you call it next line again.

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

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

[self.tableView reloadData];

and switch the both statements to

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

and maybe you don't even need to call reloadData here if only changing Color and you hold the label as property

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM