2011-10-24 69 views
-2

我是iPhone應用程序的新程序員.....我想在表三個單元格(這是不同的高度)不同的單元格高度... iphone

我寫此代碼task..and輸出是令人滿意的......但我不知道..這是正確的方法或不.....提前

感謝..... :)

CGRect frame=CGRectMake(5, 43, 311, 363); 

UITableView *table=[[UITableView alloc]initWithFrame:frame style:UITableViewStylePlain]; 

table.dataSource=self; 

table.delegate=self; 



-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath 
{ 
    static int counter=1; 

    if(counter==1) 
    { 
     counter=counter +1; 
     return 163; 

    } 
    else if(counter ==2) 
    { 
     counter=counter +1; 
     return 80; 

    } 
    else 
    { 
     counter=counter +1; 
     return 60; 

    } 
    if(counter==4) 
    { 
     counter=1; 
    } 

} 

回答

0

不,這是不對的。使用indexPath.row而不是您的counter變量。這將是您的代表被問到的行號。例如,向上滾動時,您可能無法獲得當前代碼的預期結果。

+0

@jrturton ....非常感謝... – GauravBoss

0

請勿使用

static int counter=1; 

從NSIndexPath獲取row

int row = indexPath.row; 
+0

@Lanc的...謝謝... :) – GauravBoss

0

你的static int counter不是全局變量,所以你的代碼無法工作。 它會工作,如果你從它製作伊娃,但這不是解決這個問題的正確方法。 由heightForRowAtIndexPath:委託方法提供的indexPath變量包含實際項目的索引。請在代碼中調用indexPath.row(這是實際的計數器),而不是您的計數器。

+1

@yinkoou ...感謝您的幫助... :) – GauravBoss

0

從你的代碼,我猜你有3行高度旋轉,所以最好的辦法就是用int row = indexPath.row % 3;這會給你一個循環0,1,2的值,而不是測試你的櫃檯

+0

無論如何,有更好的方法來處理整個「身高問題」也許使用一個float數組來存儲不同的值。 –

+0

@謝謝你的建議 – GauravBoss

相關問題