2012-02-28 18 views
2

私はテーブルビューでカスタムセルを設定するときにコードが重複しないようにする方法を見つけようとしています。私は現在、UITableViewControllerからサブクラス化されたBaseCellクラスと、BaseCellの2つのサブクラスである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を回避することができますあなたのIFS内の追加の変数を宣言することにより、R:

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だけである。 –

+0

@ChristopherOldfield正方形のバックセット構文を使用するか、ifの内部に一時変数を導入することでキャストを避けることができます(私の最後の編集を参照)。 – dasblinkenlight

+0

このようにすると、それぞれのif文の中に設定したすべてのプロパティに対してキャストする必要があります。たとえば、 '[[cell author] setText:]'を書いたところでは、 '[[(CellTypeOne *)cell author] setText:// author]'がなければ動作しません。おそらく、私が欲しいと思っていることにこれにアプローチする方法はありません。私は主に、(idから、またはBaseCellから)セルを実際のサブクラスにキャストしたいだけです。 –

関連する問題