2012-09-09 79 views
0

我使用的UITableView和我面臨的cellforrow代表一個問題,我用這個代碼的UITableView細胞創作上滾動

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
static NSString *CellIdentifier = @"mycell"; 
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 
    cell = (CustomCell *)[topLevelObjects objectAtIndex:2]; 
    cell.btn_Selected.hidden=YES; 
    cell.btn_Selected.tag=indexPath.row+1; 

    [array_btnContainer addObject:cell.btn_Selected]; 

} 

我的問題是,當我運行程序的時候,加載我的表視圖,並創建細胞,但是當我滾動我的表格視圖它創建一個更多的細胞爲什麼? 它必須重用已經創建的單元格,而不是輸入(cell == nil)塊,但是當我滾動它時創建一個單元格並重用其他單元格爲什麼?我卡住了

+0

當應用程序啓動時有多少行可見,以及滾動時有多少個屏幕? – rdelmar

+0

你能告訴我們這個問題嗎? – viral

回答

0

這可以通過以下步驟

  1. 創建.h文件中一個你CustomCell的參考,您正在使用,以顯示tableview中不管它是什麼讓把它稱爲ShowTableView.h來實現。 IBOutlet CustomCell * cell;

  2. 轉到CustomCell.xib並選擇文件所有者,然後將類屬性設置爲ShowTableView。

  3. 附加與CustomCell

  4. 單元格引用選擇CustomCell,然後設置屬性標識符值與了myCell

  5. 現在去你ShowTableView.m文件的cellForRowAtIndexPath方法將此代碼:

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath 
    { 
    
    static NSString *kCellIdentifier = @"mycell"; 
    cell =(CustomCell *)[tableView dequeueReusableCellWithIdentifier:kCellIdentifier]; 
    if (!cell) 
    { 
    [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];  
    } 
    } 
    

現在它將重用您以前的單元格

0

在CustomCell.m中,您必須覆蓋以下方法。

-(NSString*) reuseIdentifier{ 
     return @"mycell"; 
} 
+0

沒有解決我的問題 –

+0

是否有可能我停止創建更多的細胞後,滾動和強制使用舊的細胞 –

+0

你創造了多少個細胞?即在一個部分的行數 – andyPaul