2012-07-15 83 views
4

我有一個桌面視圖,我在故事板中設計了多個原型單元格,但是我堅持使用高度問題,因爲我的第一個單元格與第二個單元格不同等等......每個單元格都有不同的標識符,因爲我在故事板中設計了它們,我知道它們是高度的。我有這個在我的代碼,但it's沒有工作,沒有人知道如何解決它?:在不同的部分設置不同的行高(故事板)

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

UITableViewCell *cell = [[UITableViewCell alloc]init]; 
switch (indexPath.section) 

{ 
    case 1: 

     cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"]; 
     return 743.0f; 

     break; 

    case 2: 

     cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"]; 
     return 300.0f; 



} 

}

感謝您的時間。

回答

7

看起來你要使用這種方法的目的,不是爲... 你要覆蓋的方法:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    switch (indexPath.section) 
     case 1: 
      static NSString *CellIdentifier = @"cell1"; 
      break; 
     case 2: 
      static NSString *CellIdentifier = @"cell2"; 
      break; 

    UITableViewCell *cell = [tableView 
     dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell... 
    return cell; 
} 

只有改變行高在heightForRowAtIndexPath :

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

switch (indexPath.section) 

{ 
    case 1: 

     return 743.0f; 

     break; //technically never used 

    case 2: 

     return 300.0f; 



} 

退房本教程http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1它的一個很好的資源

+1

謝謝! Bandokal,問題終於解決了。我也沒有提到,但它給了我另一個問題(知道解決了!),每當我進入編輯模式(並且我有多個分組部分)時,當我想要另一行(按下小綠加按鈕)時,行的內容都搞砸了..我的意思是創建的新行(第一部分743.0f的副本),站在第二部分的頂部......知道一切正常,再次感謝你。 – Japa 2012-07-17 09:22:09

+1

歡迎來到Stack Overflow(SO)Japa!由於BandoKal回答了您的問題,請務必點擊他答案旁邊的複選標記,以便爲他提供幫助!此外,一旦你擁有10點聲望,你也可以投出所有有助於擊中向上箭頭的答案,這也給了那些花時間幫助你聲望的人! – lnafziger 2012-08-01 16:39:28