2016-04-27 8 views
0

私は自分のコントローラの1つにtableviewを持っています。タグ付きのカスタムラベルとイメージビューにデータを読み込んでいません。私はそれぞれのことをチェックし、デリゲートが添付され、viewWillAppearでデータをリロードしています。私は理解していない、どこに問題がある。私はラベルとイメージを追加し、タグを割り当てました。 tableviewのデフォルトラベルのみが表示されます。UITableViewがTabBarコントローラでデータを読み込まない

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 
+0

はcellForRowがコールしていますか? –

+0

はい、それが呼び出されます。 – WasimSafdar

+0

ラベルと画像CGrectMake(x、y、width、height) –

答えて

1

、私はそれらのいくつかは多分無効かnilであると思います。それらのいくつか無効か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 ? 

は、あなたがこのような何かを行うことができます。
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]; 

はそれが役に立てば幸い。

+0

「name = nil」と表示されていますが、無効ですがなぜですか? – WasimSafdar

+0

として、私はタグプロパティを使用していて、セル内のカスタムラベルをコンセントに接続することもできません。 – WasimSafdar

+0

このようにセルtextLabelを使用することができます:cell.textLabel.text = [historyArray objectAtIndex:indexPath.row]; – arrfu

0

xibファイルを使用してカスタムUITableViewCellを作成しましたか? もしそうなら、それをviewdidloadに登録することができます。この

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

よう 何かがその後、あなたのテーブルビューは、カスタムビュー店を見つけることができます。

希望これはあなたのUILabelとUIImageViewでカスタムのUITableViewCellを使用していないのはなぜ

+0

いいえ、オブジェクトライブラリからtableViewセルだけです。 – WasimSafdar

+0

標準の場合は、UITableViewCellのtitleプロパティにテキストを挿入します。ラベルや画像ビューを追加しないほうがいいでしょう。表示されません。 (あるいは、私はあなたがそれをやろうとする方法について誤解したかもしれません) – FilipD

+0

彼らが現れます。デフォルトラベルはUITableViewCellに表示されます。 – WasimSafdar

0

に役立ちますか?

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

これは私の必要条件ではありません。 – WasimSafdar

関連する問題