【问题标题】:Custom UITableViewCell with data from database使用数据库中的数据自定义 UITableViewCell
【发布时间】: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


【解决方案1】:

你在这个方法中得到任何数据吗..?

-(void) getData:(NSData *) data

检查数组并打印其中的每个字典..

【讨论】:

    【解决方案2】:

    问题就在这里:

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

    在上面的代码中,你设置了一个出队或空的 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 年的历史了,它是今天最后一次更新的,这就是我发布答案的原因。如果这对任何人都有好处,那就值得了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-06
      • 1970-01-01
      相关资源
      最近更新 更多