2013-08-25 19 views
-1

我正在做报纸应用程序。如何将UITableview单元格定制为子类化

我希望有一个tableview中的第一小区由2次不同规模的,并从第二小区,他们希望在每个单元和行动同样大小的3视图的每一个细胞的看法

http://imgh.us/custom_cell.png

+2

你有什么至今不工作呢? –

+0

我已经在滚动视图中创建了它,在for循环中创建视图,它对我来说工作正常,但是在解析evrytime时,它在for循环中创建视图,因此显示页面需要很长时间..... @ XCode Monkey – KSR

回答

0

只是设计的UITableViewCell的NIB文件,并创建相对的.h和.m文件,比方说:

MyCell.h 
MyCell.m 
MyCell.xib 

在MyCell.xib地方所有你想要的子视图,并设置类主单元对象为MyCell(而不是标准UITableViewCell)。
然后,您可以在代码中设置一些IBOutlet,并将它们链接到XIB中的子视图。 您也可以在自定义视图类中放置一些IBAction,虽然这是不好的做法,您应该在控制器中确实拥有自己的逻辑。 MyCell.m文件应该用于初始化逻辑和动画。

最后,在TableViewController钩一起:

#import "MyCell.h" 

#define k_CELL_ID @"k_CELL_ID" 
#define CELL_HEIGHT 80.0f 

@implementation MyTableViewController 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UITableView *theTableView = (UITableView*)self.view; 
    UINib *cellNib = [UINib nibWithNibName:@"MyCell" bundle:nil]; 
    [theTableView registerNib:cellNib forCellReuseIdentifier:k_CELL_ID]; 

    theTableView.rowHeight = CELL_HEIGHT; //not sure if this is ok in iOS 7 
} 

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath 
{ 
    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:k_CELL_ID]; 
    if (cell == nil) 
     NSLog(@"cell is nil! WTF??"); 

    id someData = //retrieve customization data 
    [cell setupWithCustomData:someData]; 

    return cell; 
} 

@end 
+0

只是真正的offtopic:他为什么不使用IB:http://blog.teamtreehouse.com/why-i-ont-use-interface-builder – CarlJ

相关问题