自定义单元格无法在ios 6的表视图中显示

时间:2013-04-05 13:21:38

标签: iphone ios ipad uitableview

#import "MasterViewController.h"
#import "DetailViewController.h"

@interface MasterViewController ()
@end

@implementation MasterViewController

 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
     if (self) {
          self.title = NSLocalizedString(@"Master", @"Master");
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        self.clearsSelectionOnViewWillAppear = NO;
        self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
    }
  }
  return self;
 }

   - (void)dealloc
   {
      [_detailViewController release];

    [super dealloc];
    }

   - (void)viewDidLoad
    {
       [super viewDidLoad];

     }

    - (void)didReceiveMemoryWarning
     {
         [super didReceiveMemoryWarning];
         // Dispose of any resources that can be recreated.
      }



     #pragma mark - Table View

        - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
         {
               return 1;
         }

        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
         {
                return 5;
          }
         -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{


    return 100.0;
         }

       // Customize the appearance of table view cells.
        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
       {
             static NSString *CellIdentifier = @"CustomCell";
             CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier: CellIdentifier];

            if (cell == nil)
          {
               cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

          }

    cell.cellDate.text=@"date";
     cell.cellDescription.text =@"Description";
     cell.cellImageview.image = [UIImage imageNamed:@"facebook.png"];
      cell.cellTitle.text = @"Title";
      return cell;


    }

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    if (!self.detailViewController) {
        self.detailViewController = [[[DetailViewController alloc] initWithNibName:@"DetailViewController_iPhone" bundle:nil] autorelease];
    }
       [self.navigationController pushViewController:self.detailViewController animated:YES];
}
    else
   {

   }
  }

@end

.h文件

    #import <UIKit/UIKit.h>
    #import "CustomCell.h"
    #import "XMLStringFile.h"

  @class DetailViewController;

 @interface MasterViewController : UITableViewController{


 }

 @property (strong, nonatomic) DetailViewController *detailViewController;
 @property(strong,nonatomic)CustomCell *customCell;
  @end

CustomCell.h


  #import <UIKit/UIKit.h>

  @interface CustomCell : UITableViewCell{

   IBOutlet UIImageView *cellImageview;
   IBOutlet UILabel *cellTitle; 
   IBOutlet UILabel *cellDescription;
   IBOutlet UILabel *cellDate;

 }
 @property (retain, nonatomic) IBOutlet UIImageView *cellImageview;
 @property (retain, nonatomic) IBOutlet UILabel *cellTitle;
 @property (retain, nonatomic) IBOutlet UILabel *cellDescription;
 @property (retain, nonatomic) IBOutlet UILabel *cellDate;

 @end

CustomCell.m


      #import "CustomCell.h"

      @implementation CustomCell

    @synthesize cellDate;
   @synthesize cellDescription;   
  @synthesize cellImageview;
 @synthesize cellTitle;

 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    // Initialization code
}
return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{
[super setSelected:selected animated:animated];

// Configure the view for the selected state
}

- (void)dealloc {
    [super dealloc];
 }
@end

  

这是我的同班同学    这里问题出现在customcell cant显示onlt白色屏幕的tableview数据中。    在这里我使用masterdetails视图模板在ios中工作..    这里我还在编译源中添加了m文件    Customcell.xib大小为300X100    这是我的输出    我的xib文件如下   This is my CustomCell.xib

This is my output Screen

请帮我解决问题

3 个答案:

答案 0 :(得分:2)

 if(cell == nil)
 {
     NSArray *outlets = [NSArray arrayWithArray:[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]];
     for(id obj in outlets)
     {
         if([obj isKindOfClass:[UITableViewCell class]])
         {
             cell = (CustomCell *)obj;
         }
     }
 }

如果您的CustomCell是通过XIB创建的,则将此代码写入您的单元格nil

修改: -
这可能是原因 - 我认为您还没有更改CustomCell的班级名称。请按照以下步骤进行操作 -

  1. 使用名称为XIB的{​​{1}},然后删除其视图。
  2. 取一个CustomCell.xib并根据你的身高和结构设置。
  3. 选择UITableViewCell并将其类名更改为File's Owner,与CustomCell相同...选择它并将其类名更改为UITableViewCell
  4. 现在连接所有CustomCell IBOutLets
  5. 注意: - 右键点击subView而不是IBOutLets ,选择UITableViewCell

答案 1 :(得分:2)

如果单元格是在笔尖中设计的,那么您需要从笔尖加载单元格。

自iOS 5以来,这实际上在UIKit支持

您需要做的是使用UITableView注册您的笔尖。

- (void)viewDidLoad
{
  [super viewDidLoad];

  [self.tableView registerNib:[UINib nibWithNibName:@"Customcell" bundle:nil]   
       forCellReuseIdentifier:@"CustomCell"];

  //...
}

现在你的tableView:cellForRowAtIndexPath:只需要看起来像这样

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *CellIdentifier = @"CustomCell";
  CustomCell *cell = (id)[tableView dequeueReusableCellWithIdentifier: CellIdentifier];

  cell.cellDate.text        = @"date";
  cell.cellDescription.text = @"Description";
  cell.cellImageview.image  = [UIImage imageNamed:@"facebook.png"];
  cell.cellTitle.text       = @"Title";

  return cell;
}

NB

请务必记住将CustomCell设置为xib中的reuseIdentifier

其他一些观察

  • 你真的应该将ARC用于新项目。
  • @synthesize是隐含的,因此在大多数情况下不需要
  • #import "CustomCell.h"中的MasterViewController.h可能不太好。 您应该将其移至.m并使用.h中的转发声明代替 - @class CustomCell;
  • 您也不需要为属性声明支持ivars,因为这些也将由编译器生成,例如:您不需要声明以下内容

    @interface CustomCell : UITableViewCell {
      IBOutlet UIImageView *cellImageview;
      IBOutlet UILabel *cellTitle; 
      IBOutlet UILabel *cellDescription;
      IBOutlet UILabel *cellDate;
    }
    

答案 2 :(得分:2)

在viewDidLoad

中执行以下操作
- (void)viewDidLoad
{
   [super viewDidLoad];
   UINib *nib = [UINib nibWithNibName:@"Customcell" bundle:nil];
   [[self tableView] registerNib:nib forCellReuseIdentifier:@"CustomCell"];
}