繁体   English   中英

无法加载Objective-C自定义UITableView类

[英]Objective-C custom UITableView class doesn't load

在XCode 5中,我以Tab-Bar示例开始,并在故事板上的UIController中添加了UITableView。 我想为该UITableView使用一个自定义类,因此我在项目中添加了一个新类,并将UITableView的类设置为情节提要中的该新类。 我还设置了cellIdentifier。 问题在于该应用程序仍在使用默认的UITableView类。 你能告诉我我想念的东西吗?

这是我的自定义UITableView类

#import <UIKit/UIKit.h>
@interface CustomTableView : UITableView <UITableViewDelegate, UITableViewDataSource>
@end

@implementation CustomTableView

- (CustomTableView*) init {
  self = [super init];
  if (self) {
    self.delegate = self;
    self.dataSource = self;
    self.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    [self reloadData];
  }
  return self;
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return 20;
}

- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  return @"header title";
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString* staticCellIdentifier = @"customIdentifier";
  UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:staticCellIdentifier];
  if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:staticCellIdentifier];
  }
  cell.textLabel.text = @"cellTitle";
  return cell;
}
@end

您需要实现initWithCoder:这是构建器指定的初始化程序接口使用。

像这样:

- (CustomTableView*) initWithCoder:(NSCoder *)aDecoder{
  self = [super initWithCoder:(NSCoder *)aDecoder];
  if (self) {
    self.delegate = self;
    self.dataSource = self;
    self.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    [self reloadData];
  }
  return self;
}

暂无
暂无

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

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