我在應用程序中使用靜態單元格的UITableViewController
。有沒有什麼辦法可以在表視圖中使用默認單元格以及代碼中的子類單元格?我的tableview有8行,其中6行希望在tableview中使用默認單元格。對於剩餘的兩個單元格,我想通過代碼創建它。使用靜態單元格對Tableview中的單元格進行子類化
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:@"MyCustomCell"];
if (cell == nil) {
cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:@"MyCustomCell"];
}
return cell;
}
而在MyCustomCell.m包含,
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
self.myLabel.backgroundColor = [UIColor clearColor];
self.myLabel.font = [UIFont boldSystemFontOfSize:[UIFont smallSystemFontSize]];
self.myLabel.textAlignment = NSTextAlignmentCenter;
self.myLabel.text = @"Hi there";
[self.contentView addSubview:self.myLabel];
}
return self;
}
的-tableView:CellForRowAtIndexPath:
方法有助於創建custom cells
的代碼,但我不知道知道如何訪問默認cells
這裏,如果它是可能的。
你爲什麼不在故事板上做? –
這個的實際目的是實現在線拾取器單元。從故事板延遲加載視圖(3-4秒),加載更多的撿取器(在我的情況下是4個拾取器)。我不想這樣做。 – Sibin
從Storyboard加載2個細胞是不可能的,需要3-4秒。如果你真的想要避免Storyboard,你可以將它們放在xib文件中。 – Szu