2014-07-22 215 views
1

我在應用程序中使用靜態單元格的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這裏,如果它是可能的。

+0

你爲什麼不在故事板上做? –

+0

這個的實際目的是實現在線拾取器單元。從故事板延遲加載視圖(3-4秒),加載更多的撿取器(在我的情況下是4個拾取器)。我不想這樣做。 – Sibin

+0

從Storyboard加載2個細胞是不可能的,需要3-4秒。如果你真的想要避免Storyboard,你可以將它們放在xib文件中。 – Szu

回答

1

您可以使用indexPath.row屬性來查看您的目標行是否小於第6行,如果是這種情況,則將不是您的自定義單元的單元出列。

首先創建自定義細胞,並給它的標識符(「myCustomCellReuseId)然後在您的視圖控制器使用:

[tableview registerNib:[UINib nibWithName:@"CustomCell" bundle:nil] forCellReuseIdentifier:@"myCustomCellReuseId"]; 

然後,在原型細胞在故事板,得到默認細胞中比不同的標識符你給你的自定義單元格的一個

然後,在你-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath使用:

if(indexPath.row > 5) { 
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCustomCellReuseId"]; 
} else { 
    // 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 

} 
+0

我不知道該怎麼做。在我的情況下,前6行必須是故事板的默認單元格,其餘2個單元格是代碼。可能嗎?如果可以,請提供代碼。謝謝。 – Sibin

+0

請參閱我編輯的答案。 – KerrM

+0

我知道如何使用動態細胞類型。我正在嘗試使用所有的靜態單元格,這使我無法編寫大量代碼。從所有的迴應中我都認爲在這種情況下不可能使用靜態單元格。我想我的問題不清楚:P。感謝您的幫助。 – Sibin

1

由於@Sviatoslav Yakymiv提到的設計單元格的最簡單方法是將Storyboard與程序化自定義混合在一起:您將在Storyboard中完成的佈局,但是您將在視圖控制器.m文件中更改的內容。這意味着您在-initWithStyle: reuseIdentifier:中的所有代碼都可以在IB中進行設計。然後,您可以在IB中創建2個不同的動態原型。換句話說,您可以將您的自定義單元格與默認的UITableViewCell混合。例如在Interface Builder你有動態原型細胞:

  1. 默認的UITableViewCell與[email protected]"Cell"

  2. [email protected]"MyCustomCell"自定義單元格(您可以在右上角的Indentity Inspector中更改它)。

如果你會做正確,您將不LOGEN需要使用這3行:

if (cell == nil) { 
     cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:@"MyCustomCell"]; 
    } 

那麼您應該將功能更改爲:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.row >= rowWhereCustomCellShouldShow && indexPath.section > yourSection) { 
     MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:@"MyCustomCell"]; 
     [cell customizeForData:data[indexPath.row]; 
     return cell; 
    } else { // The apple default cell 
     UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
     // Here you can customize your cell. 
     return cell2; 
    } 
} 
+0

我知道如何使用動態細胞類型。我試圖在這種情況下使用所有靜態單元來避免額外的代碼。從所有的迴應中我都認爲在這種情況下不可能使用靜態單元格。謝謝您的幫助。 – Sibin