1
#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

自定义单元格.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 的tableview 数据中无法在白屏上显示。在这里,我使用 masterdetails 视图模板在 ios 中工作。这里我还在编译源中添加了 m 文件 Customcell.xib 大小为 300X100 这里我的输出我的 xib 文件如下 这是我的 CustomCell.xib

这是我的输出屏幕

请帮我解决问题

4

3 回答 3

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名字CustomCell.xib然后删除它的视图。
  2. 取一个UITableViewCell并根据你设置它的高度和结构。
  3. 选择File's Owner并将其类名更改为CustomCell,与...相同的操作UITableViewCell选择它并将其类名更改为CustomCell
  4. 现在连接所有subViewIBOutLets

注意:- 通过右键单击not from选择IBOutLetsUITableViewCellFile's Owner

于 2013-04-05T13:26:56.413 回答
2

如果电池设计在笔尖中,那么您需要从笔尖加载电池。

这实际上UIKit从 iOS 5开始支持

您需要做的是将您的笔尖注册到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;
}

注意

一定要记得CustomCell在xib中设置为reuseIdentifier

其他一些观察

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

    @interface CustomCell : UITableViewCell {
      IBOutlet UIImageView *cellImageview;
      IBOutlet UILabel *cellTitle; 
      IBOutlet UILabel *cellDescription;
      IBOutlet UILabel *cellDate;
    }
    
于 2013-04-05T13:35:48.370 回答
2

在 viewDidLoad 中执行以下操作

- (void)viewDidLoad
{
   [super viewDidLoad];
   UINib *nib = [UINib nibWithNibName:@"Customcell" bundle:nil];
   [[self tableView] registerNib:nib forCellReuseIdentifier:@"CustomCell"];
}
于 2013-04-05T13:36:16.847 回答