簡體   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