简体   繁体   English

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

[英]Custom Cell can't display in Table view in ios 6

#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 file .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 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 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

This is my both class here Problem is in tableview data of the customcell cant Display onlt white screen. 这是我的两个班级问题在customcell的tableview数据中无法显示白色屏幕。 Here i work in ios with masterdetails view template.. here i have also added m file in compile source Customcell.xib size is 300X100 Here my output My xib file as below 在这里我使用masterdetails视图模板在ios中工作。在这里我还在编译源Customcell.xib中添加了m文件,大小为300X100。在这里,我的输出如下: 这是我的CustomCell.xib

这是我的输出屏幕

please help me to solve problem 请帮我解决问题

 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;
         }
     }
 }

If your CustomCell is made via XIB then Write this Code where your cell is nil . 如果您的CustomCell是通过XIB则在您的单元格为nil地方编写此代码。

EDIT:- 编辑:-
This may be the cause - I think you haven't changed your CustomCell 's class name. 这可能是原因-我认为您尚未更改CustomCell的类名。 Follow these steps - 跟着这些步骤 -

  1. Take a XIB with name CustomCell.xib then delete its view. 取一个名称为CustomCell.xibXIB ,然后删除其视图。
  2. Take a UITableViewCell and set its height and structure according to you. 取一个UITableViewCell并根据您的需要设置其高度和结构。
  3. Select File's Owner and change its class name to CustomCell , Same thing do with UITableViewCell ... select it and change its class name to CustomCell . 选择File's Owner并将其类名更改为CustomCell ,与UITableViewCell相同……选择它,并将其类名更改为CustomCell
  4. Now connect all subView 's IBOutLets . 现在连接所有subViewIBOutLets

Note:- Select IBOutLets by right clicking on UITableViewCell not from File's Owner . 注意:- 右键单击UITableViewCell而不是从File's Owner选择IBOutLets

If the cell is designed in a nib then you need to load the cell from the nib. 如果单元是在笔尖中设计的,则需要从笔尖加载单元。

This actually has support in UIKit since iOS 5 自iOS 5起,它实际上已在UIKit得到支持

What you need to do is register your nib with the UITableView . 您需要做的是在UITableView注册您的笔尖。

- (void)viewDidLoad
{
  [super viewDidLoad];

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

  //...
}

Now your tableView:cellForRowAtIndexPath: just needs to look like this 现在您的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 NB

Make sure to remember to set CustomCell as the reuseIdentifier in the xib 确保记住在CustomCell设置为复用标识符

Some other observations 其他一些观察

  • Really you should be using ARC for a new project. 确实,您应该将ARC用于新项目。
  • @synthesize is implicit so not required in most cases @synthesize是隐式的,因此在大多数情况下不是必需的
  • It's probably a bad idea to #import "CustomCell.h" in MasterViewController.h . MasterViewController.h #import "CustomCell.h"可能不是一个好主意。 You should move this to the .m and use a forward declaration in the .h instead - @class CustomCell; 您应该将其移至.m并在.h使用前向声明- @class CustomCell;
  • You also do not need to declare the backing ivars for properties as these will also be generated by the compiler eg you don't need to declare the following 您也不需要声明属性的支持ivars,因为这些属性也将由编译器生成,例如,您不需要声明以下内容

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

Do the following in viewDidLoad 在viewDidLoad中执行以下操作

- (void)viewDidLoad
{
   [super viewDidLoad];
   UINib *nib = [UINib nibWithNibName:@"Customcell" bundle:nil];
   [[self tableView] registerNib:nib forCellReuseIdentifier:@"CustomCell"];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM