简体   繁体   English

UITableView无法使用TabBar控制器加载数据

[英]UITableView not loading data with TabBar Controller

I have tableview in one of my controller. 我有一个控制器中的tableview。 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. 我检查了每件事,都附加了委托,并在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. 仅显示tableview的默认标签。

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? 您是否使用xib文件创建了自定义UITableViewCell? If so, you could register it in viewdidload. 如果是这样,您可以在viewdidload中注册它。 Something like this 像这样

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

Then your tableview can find your custom view outlets. 然后,您的tableview可以找到您的自定义视图出口。

Hope this helps 希望这可以帮助

Why don't you use a custom UITableViewCell with your UILabel and UIImageView? 为什么不将自定义UITableViewCell与UILabel和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. 无论如何,您的UILabel和UIImage看起来都在获取值,但并未将其设置为您的单元格。 To test you can log the value of the UILabel just to check. 要进行测试,您可以记录UILabel的值以进行检查。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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