2012-01-20 10 views
0

私はカスタムUITableViewCellを作成しています。だから、アイデアはそれのニュースのいくつかの種類とUITableViewを作ることです。このニュースにはトピックと説明があります。私はNieuwsTableviewCellクラスと、UITableViewControllerであるFirstViewControllerクラスを持っています。私はデータベースから自分のデータを取得するためにPHPファイルを使って作業しています。 ラベルの新しい行ごとにnews_topicとdescriptionを取得するにはどうすればよいですか?データベースからのデータを含むカスタムUITableViewCell

私のFirstViewController.mファイルは次のようになっています。

#import "FirstViewController.h" 
#import "NieuwsTableViewCell.h" 

@implementation FirstViewController 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 
-(void) getData:(NSData *) data{ 

    NSError *error; 

    json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

    [self.tableView reloadData]; 
} 
-(void) start { 

    NSURL *url = [NSURL URLWithString:kGETUrl]; 

    NSData *data = [NSData dataWithContentsOfURL:url]; 

    [self getData:data]; 

} 
- (void)viewDidLoad 
{ 
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; 
    [super viewDidLoad]; 
[self start]; 

} 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
#warning Potentially incomplete method implementation. 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
#warning Incomplete method implementation. 
    // Return the number of rows in the section. 
    return [json count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    NieuwsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 

     NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"NieuwsTableViewCell" owner:nil options:nil]; 

     for (UIView *view in views) { 
      if([view isKindOfClass:[UITableViewCell class]]) 
      { 
       NSDictionary *info = [json objectAtIndex:indexPath.row]; 
       cell.textLabel.text = [info objectForKey:@"Nie_topic"]; 
       cell = (NieuwsTableViewCell*)view; 
      } 
     } 
    } 

    return cell; 
} 

FirstViewController。 Hファイル

#define kGETUrl @"http://localhost/getNieuws.php" 
@interface FirstViewController : UITableViewController{ 
    NSMutableArray *json; 
} 

そして、これが

@interface NieuwsTableViewCell : UITableViewCell{ 
    IBOutlet UILabel *topic; 
    IBOutlet UILabel *omschrijving; 

} 
@property (nonatomic, retain) IBOutlet UILabel *topic; 
@property (nonatomic, retain) IBOutlet UILabel *omschrijving; 

@end 

私NieuwsTableViewCell.hファイルでは、誰もが助けてもらえますか?

+0

どこに正確に貼り付けられていますか?実行時に画面上で何を取得しますか? [json count]は正しい値を返しますか?私はステップバイステップを進めることをお勧めします:まず、標準のUITableViewCell(xibからロードされていない)を試すことができます。それが動作する場合は、カスタムセルを導入することができます。そうでない場合、その問題はおそらくデータに関連しています。 –

+0

ちょうど終了ヒントは、セルがnilの場合のみ、セルのtextLabelを設定しています。デキューされているセルがない場合は表示されません。 – Justin

答えて

0

このメソッドでデータを取得していますか?

-(void) getData:(NSData *) data 

チェック配列&プリントその中の各辞書..

0

問題はここにある:上記のコードで

NSDictionary *info = [json objectAtIndex:indexPath.row]; 
cell.textLabel.text = [info objectForKey:@"Nie_topic"]; 
cell = (NieuwsTableViewCell*)view; 

、あなたがデキューまたはnullのUITableViewCellのプロパティを設定しています、セル変数を再設定して、textLabelに行った変更を上書きして、トピックを表示させないようにします。

次のコードを検討してください。現時点で使用しているコードとは対照的に、tableViewが再利用可能なセルをまだ作成していない場合はifステートメントが呼び出され、ロードしているnibのUITableViewCellサブビューにセルが設定されます。セルがnilのときにtextLabelのテキストを設定するのではなく、再利用の問題やセルにトピックが表示されないように、すべての単一セルに対してこれを実行します。

NieuwsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 
    NSArray *subViews = [[NSBundle mainBundle] loadNibNamed:@"NieuwsTableViewCell" owner:nil options:nil]; 

    for (UIView *view in subViews) { 
     if([view isKindOfClass:[UITableViewCell class]]) 
     { 
      cell = (NieuwsTableViewCell*)view; 
     } 
    } 
} 
NSDictionary *info = [json objectAtIndex:indexPath.row]; 
cell.textLabel.text = [info objectForKey:@"Nie_topic"]; 

編集---ちょうどこのポストが〜1歳であることに気付きました。今日は最後に更新されたので、私は答えを投稿しました。これが誰にも利益をもたらすなら、それはそれに値するものでした。

関連する問題