2012-02-28 69 views
2

我試圖找到一種方法,當我在我的表視圖中設置自定義單元格時沒有代碼被重複。我目前有一個BaseCell類,從UITableViewController繼承,然後是BaseCell的兩個子類:CellTypeOne和CellTypeTwo。這是哪門子的設置我使用的時刻:UITableViewCell的子類(可能使用id)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *type = [self.rowTypes objectAtIndex:indexPath.row]; 

    if ([type isEqualToString:@"Type 1"]) { 
     CellTypeOne *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell 1"]; 

     cell.picture.image = // An image 
     cell.caption.text = // A caption 

     cell.author.text = // An author 

     return cell; 
    } 

    else if {[type isEqualToString:@"Type 2"]) { 
     CellTypeTwo *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell 2"]; 

     cell.picture.image = // An image 
     cell.caption.text = // A caption 

     cell.number.text = // A number 

     return cell; 
    } 
} 

由於有些特性是集都是相同的兩種細胞類型,我想知道是否有一種方法可以做這樣的事情:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *type = [self.rowTypes objectAtIndex:indexPath.row]; 

    id cell; 

    if ([type isEqualToString:@"Type 1"]) { 
     cell = [tableView dequeueReusableCellWithIdentifier:@"Cell 1"]; 

     // Cast cell to (CellTypeOne *)? 

     cell.author.text = // An author 
    } 

    else if {[type isEqualToString:@"Type 2"]) { 
     CellTypeTwo *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell 2"]; 

     // Cast cell to (CellTypeTwo *)? 

     cell.number.text = // A number 
    } 

    cell.picture.image = // An image 
    cell.caption.text = // A caption 

    return cell; 
} 

這樣我可以爲每個子類做類的具體設置,然後做兩個通用的設置。我已經得到這個工作的唯一方法是通過每次鑄造池中我需要使用它,像這樣:

[(BaseCell *)cell picture].image = // An image 

有投細胞在每行像更多的工作似乎比有幾行重複的代碼開始,但我只是想找出最好的方式去做這件事。

回答

2

您可以BaseCell*型,避免了cell可變蒙上外IFS:

BaseCell *cell; 

if ([type isEqualToString:@"Type 1"]) { 
    cell = [tableView dequeueReusableCellWithIdentifier:@"Cell 1"]; 
    [[cell author] setText:/*an author*/]; 
} else if {[type isEqualToString:@"Type 2"]) { 
    cell = [tableView dequeueReusableCellWithIdentifier:@"Cell 2"]; 
    [[cell number] setText: /* a number */]; 
} 

cell.picture.image = // An image 
cell.caption.text = // A caption 

或者,您也可以保持id的類型和使用方法調用語法,而不是成員(點)語法的IFS:

id cell; 
// ... the code from your post 
[[cell picture] setImage: /* an image */]; 
[[cell caption] setText: /* a caption */]; 

最後,你能避免鑄件方括號altogethe [R通過聲明你的IFS內額外的變量:

BaseCell *cell; 

if ([type isEqualToString:@"Type 1"]) { 
    CallTypeOne *cellOne = [tableView dequeueReusableCellWithIdentifier:@"Cell 1"]; 
    cellOne.author.text = // An author 
    cell = cellOne; 
} else if {[type isEqualToString:@"Type 2"]) { 
    CellTypeTwo *cellTwo = [tableView dequeueReusableCellWithIdentifier:@"Cell 2"]; 
    cellTwo.number.text = // A number 
    cell = cellTwo; 
} 
+0

每個單元的具體設置裏面我還是要投,雖然,例如'cell.number.text'是行不通的,因爲數量不是一個屬性在BaseCell上,但只在CellTypeTwo上。 – 2012-02-28 03:47:34

+0

@ChristopherOldfield你可以通過使用正方形背景語法或在你的ifs中引入臨時變量來避免強制轉換(參見我的上一次編輯)。 – dasblinkenlight 2012-02-28 03:55:21

+0

這樣做,我仍然需要對每個if語句中設置的每個屬性進行強制轉換。例如,你寫''[cell author] setText:]'的地方除非是[[(CellTypeOne *)cell author] setText:// author]',否則它將不起作用。也許沒有辦法以我想要的方式來實現。我主要只是想將單元格(無論是從id還是從BaseCell)轉換爲實際的子類。 – 2012-02-28 03:55:54

相關問題