【发布时间】:2012-01-20 11:08:48
【问题描述】:
我正在制作一个自定义 UITableViewCell。所以这个想法是制作一个带有某种新闻的 UITableView。这条新闻有一个主题和一个描述。我有一个 NieuwsTableviewCell 类和一个作为 UITableViewController 的 FirstViewController。我正在使用 PHP 文件从数据库中获取我的数据。 如何获取标签上每个新行的 news_topic 和描述?
我的 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;
}
第一视图控制器。 h文件
#define kGETUrl @"http://localhost/getNieuws.php"
@interface FirstViewController : UITableViewController{
NSMutableArray *json;
}
这是我的 NieuwsTableViewCell.h 文件
@interface NieuwsTableViewCell : UITableViewCell{
IBOutlet UILabel *topic;
IBOutlet UILabel *omschrijving;
}
@property (nonatomic, retain) IBOutlet UILabel *topic;
@property (nonatomic, retain) IBOutlet UILabel *omschrijving;
@end
有人可以帮忙吗?
【问题讨论】:
-
你到底卡在哪里了?执行时在屏幕上获得什么? [json count] 是否返回正确的值?我建议您逐步进行:首先您可以尝试使用标准 UITableViewCell(不是从 xib 加载的)。如果可行,您可以介绍您的自定义单元格。如果不是,则问题可能与数据有关。
-
只是一个退出提示,只有当单元格为 nil 时,您才设置单元格的 textLabel。不是当有一个单元出队时。
标签: objective-c ios uitableview