私はカスタム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ファイルでは、誰もが助けてもらえますか?
どこに正確に貼り付けられていますか?実行時に画面上で何を取得しますか? [json count]は正しい値を返しますか?私はステップバイステップを進めることをお勧めします:まず、標準のUITableViewCell(xibからロードされていない)を試すことができます。それが動作する場合は、カスタムセルを導入することができます。そうでない場合、その問題はおそらくデータに関連しています。 –
ちょうど終了ヒントは、セルがnilの場合のみ、セルのtextLabelを設定しています。デキューされているセルがない場合は表示されません。 – Justin