UITableView不使用TabBar Controller加载数据

时间:2016-04-27 13:02:06

标签: ios objective-c uitableview

我在我的一个控制器中有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

3 个答案:

答案 0 :(得分:1)

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

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];
希望它有所帮助。

答案 1 :(得分:0)

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

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

然后您的tableview可以找到您的自定义视图插座。

希望这有帮助

答案 2 :(得分:0)

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

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