2013-07-24 44 views
0

我已經在IB中創建了一個包含2 UIButton的單元格,並對其進行了分類。我怎樣才能使用它,而不用重新使用它呢? (對於一個小的固定表) 我試着做類似: RaffleResultCell *cell = [tableView dequeueReusableCellWithIdentifier:nil]; 但這不會顯示我的細胞在UITableView,只是一個空白的。如何從IB中使用子類別的單元而不重新使用它?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
     //Where we configure the cell in each row 
     id currentRaffle = [_winnings objectAtIndex:indexPath.row]; 
     RaffleResultCell *cell = [tableView dequeueReusableCellWithIdentifier:@"raffleResCell"]; 
     if (cell == nil) { 
      cell = [[RaffleResultCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"raffleResCell"]; 
     } 
return cell; 
} 
+0

'@」無「而不是」無「? – trojanfoe

+0

錯字,謝謝... – Segev

+1

如果你不介意我的問題,是什麼讓你想避免重用?這個願望通常會在你的應用程序的設計中出現問題。 – katzenhut

回答

0

這是不是一個好的做法,以避免可重用性,我會說,不這樣做

可重用性在這一行

RaffleResultCell *cell = [tableView dequeueReusableCellWithIdentifier:@"raffleResCell"]; 

進行刪除該行並調用ALLOC方法每次沒有檢查循環

Like

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
     id currentRaffle = [_winnings objectAtIndex:indexPath.row]; 
     cell = [[RaffleResultCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"raffleResCell"]; 
     return cell; 
} 
+0

'RaffleResultCell * cell = [[RaffleResultCell alloc] init];'像這樣?我仍然得到一個空白的單元格。 – Segev

+0

您的示例中尚未配置單元格。 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath :(NSIndexPath *)indexPath RaffleResultCell * cell = [[RaffleResultCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@「raffleResCell」]; return cell;我仍然給了我一個空白的單元格。 – Segev

+0

這是你的cell的子類的問題,它沒有正確設置 –

0

你告訴你在IB設立的UITableViewCell,那麼你需要得到護理文件,然後使用該文件作爲

// Get nib file from mainBundle 
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"RaffleResultCell" owner:self options:nil]; 

    for (id currentObject in topLevelObjects) { 
     if ([currentObject isKindOfClass:[UITableViewCell class]]) { 
      RaffleResultCell *cell = (RaffleResultCell *)currentObject; 
      break; 
     } 
    } 

現在設置任何的文字作爲按鈕返回細胞

相關問題