2012-01-23 67 views
0

我的自定义tableview单元格有问题。当我想将文字放在我的标签上时,它不会改变。这是我的代码的样子。自定义UITableviewCell不会设置标签

我NieuwsTableViewCell.h

@interface NieuwsTableViewCell : UITableViewCell{ 

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

@end 

我NieuwsTAbleViewCell.m

@implementation NieuwsTableViewCell 
@synthesize topic; 
@synthesize omschrijving; 

和我firstViewController.m

- (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.topic.text = @"Stef"; 
      cell.omschrijving.text = [info objectForKey:@"Nie_omschrijving"]; 
      NSLog(@"voorlopige test"); 


      cell = (NieuwsTableViewCell*)view; 
      } 
     } 
    } 

    return cell; 
} 

回答

1

这是因为你设置里面的文本属性如果(!cell)阻止。该块只会被调用几次来创建可重用对象。以简单的方式移动单元格。*。text =部分在if(!cell)块之外。这是一个完整的代码JIC

- (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]]) 
      { 
      cell = (NieuwsTableViewCell*)view; 
      } 
     } 
    } 
       NSDictionary *info = [json objectAtIndex:indexPath.row]; 

      cell.topic.text = @"Stef"; 
      cell.omschrijving.text = [info objectForKey:@"Nie_omschrijving"]; 
      NSLog(@"voorlopige test"); 

    return cell; 
} 
+0

它工作!谢谢 ! – steaphann