简体   繁体   中英

UITableView not loading data with TabBar Controller

I have tableview in one of my controller. It is not loading data into custom labels and image view with tags. I checked each and every thing, delegates are attached and I am reloading the data in viewWillAppear. I do not understand, where is the problem. I added label and images and assign them tag. Only default label of tableview is showed.

NSString *cellIdentifier;
NSMutableArray *historyArray;

@implementation CloudHistory

-(void)viewDidLoad
{
    [super viewDidLoad];

    historyArray = [[NSMutableArray alloc]initWithObjects:@"history1",@"history2",@"history3",@"history4",nil];

}

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

    dispatch_async(dispatch_get_main_queue(), ^{
        //Reload data in services table.
        [_historyTableView reloadData];
    });
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //Number of section in services table.
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [historyArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    cellIdentifier = @"HistoryCell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

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

    UILabel *name = (UILabel*)[cell viewWithTag:40];
    name.text =[historyArray objectAtIndex:indexPath.row];
    UIImageView *image = (UIImageView*)[cell viewWithTag:44];
    image.image = [UIImage imageNamed:@"rtb_logo"];

    return cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Change the background color to stoker Cloud color.
    cell.selectedBackgroundView = [UIView new];
    cell.selectedBackgroundView.backgroundColor = [ UIColor colorWithRed:116.0/255.0 green:174.0/255.0 blue:220.0/255.0 alpha:1.0];
}

-(void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

-(BOOL)prefersStatusBarHidden
{
    return YES;
}

@end

maybe you can export the name and image , I think some of them maybe is invalid or nil .

UILabel *name = (UILabel*)[cell viewWithTag:40];
NSLog(@"name = %@",name); // is it valid ?

UIImageView *image = (UIImageView*)[cell viewWithTag:44];
NSLog(@"image = %@", image); // is it valid ?

if some of them invalid or nil,you can do something like this:

    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, 80, 44)];
    name.text = @"name";
    [cell addSubview:name];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 5, 44, 44)];
    imageView.image = image;
    [cell addSubview:imageView];

hope it helps.

Did you create a custom UITableViewCell using a xib file? If so, you could register it in viewdidload. Something like this

    [self.tableView registerNib:[UINib nibWithNibName:@"xibName" bundle:nil] forCellReuseIdentifier:CellIdentifier];

Then your tableview can find your custom view outlets.

Hope this helps

Why don't you use a custom UITableViewCell with your UILabel and UIImageView? So you can access the cell property, like:

cell.name.text = [historyArray objectAtIndex:indexPath.row];
cell.image = [UIImage imageNamed:@"rtb_logo"];

Anyway looks like your UILabel and UIImage is getting the values, but it's not setting them to your cell. To test you can log the value of the UILabel just to check. I don't know if it will work but have you tried this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    cellIdentifier = @"HistoryCell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    UILabel *name = (UILabel*)[cell viewWithTag:40];
    name.text =[historyArray objectAtIndex:indexPath.row];
    [cell viewWithTag:40] = name; // I don't know if this works

    // Check it out if the name.text is getting any value
    NSLog(@"UILabel %@",name.text);

    UIImageView *image = (UIImageView*)[cell viewWithTag:44];
    image.image = [UIImage imageNamed:@"rtb_logo"];
    [cell viewWithTag:44] = image; // I don't know if this works

    return cell;
}

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