繁体   English   中英

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

[英]UITableView not loading data with TabBar Controller

我有一个控制器中的tableview。 它不会将数据加载到自定义标签和带有标签的图像视图中。 我检查了每件事,都附加了委托,并在viewWillAppear中重新加载数据。 我不明白,问题出在哪里。 我添加了标签和图像并为其分配标签。 仅显示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

也许您可以导出名称和图像,我认为其中一些可能无效或无效。

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

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

如果其中一些无效或无效,则可以执行以下操作:

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

希望能帮助到你。

您是否使用xib文件创建了自定义UITableViewCell? 如果是这样,您可以在viewdidload中注册它。 像这样

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

然后,您的tableview可以找到您的自定义视图出口。

希望这可以帮助

为什么不将自定义UITableViewCell与UILabel和UIImageView一起使用? 因此,您可以访问单元格属性,例如:

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

无论如何,您的UILabel和UIImage看起来都在获取值,但并未将其设置为您的单元格。 要进行测试,您可以记录UILabel的值以进行检查。 我不知道它是否有效,但是您是否尝试过:

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