2012-07-13 125 views
2

我的問題如下:我指定一個可變數組cellForRowAtIndexPath,以便它顯示單元格中的每個數組對象。到目前爲止,單元格按預期顯示。現在我想要在第一個單元格中顯示一個UILabel,以便其他可變數組對象將被移動到第二個單元格,第三個等等。 問題是,當我測試該條件時,確實,UILabel顯示在第一個單元中,第一個對象。實際上,兩個元素在同一個單元格中,這不是我所期望的。我希望(條件爲真時)移動所有元素,以便它們將顯示在第二個單元格中,以便將第一個單元格留到UIlabel問題與cellForRowAtIndexPath數據源方法

我的相關代碼,並沒有給我什麼,我想到,是這樣的:在我的邏輯

- (UITableViewCell*)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 


    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"any-cell"]; 


// Add and display the Cell  
     cell.tag = [indexPath row]; 
     NSLog(@"cell.tag= %i",cell.tag); 
     //test the condition, if it's ok, then add the label to the first cell 
     if ([self isNoScoreLabelDisplayed] && cell.tag==0) { 
     UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 220, 50)]; 
     [lbl setBackgroundColor:[UIColor greenColor]]; 
     [cell addSubview:lbl]; 
     } 
    cell.tag = [self isNoScoreLabelDisplayed]?[indexPath row]+1:[indexPath row];//here i wanted to shift the tags in case the condition is true, so that all the elements will be displayed from the second cell. But seems not doing what i want :(


     // 
     if (indexPath.row < cellList.count) { 

      [cell addSubview:[cellList objectAtIndex:[indexPath row]]];//cellList is the mutable array from which i get all the elements to display in the cells 

     }else{ 

      [cell addSubview:nextButton]; 
     } 


     return cell; 
} 

我這麼想嗎? thanx提前。

+0

據我瞭解,你想的東西像頭 - 爲什麼不能用表的headerview這個? – Pfitz 2012-07-13 07:25:58

+0

嗨,thanx爲您的答覆。其實標題還在做其他事情,它顯示我的遊戲名稱。沒辦法使用標題:)) – Malloc 2012-07-13 07:29:26

+0

'細胞'被創建在哪裏?你可以擴展該代碼嗎? – 2012-07-13 07:35:50

回答

0

您可以將標籤的文本作爲第一個元素插入容器中並進行檢查。通過這種方式,您可以節省索引偏移量和代碼複雜性。

E.g.

[cellList insertObject:@"Label name" atIndex:0]; 
if ([cell tag] == 0) { 
    // add required label 
} 
else { 
    // do whatever you do for your standard cells, getting them with [cellList objectAtIndex:[indexPath row]]; 
} 
+0

Thanx回覆,請您澄清更多? – Malloc 2012-07-13 07:45:23

+0

我添加了一些代碼。 – Alexander 2012-07-13 07:50:05

+0

你的意思是//添加所需的標籤。你的意思是將標籤添加到'cell:addSubview'?像這樣:'[cell addSubview:myLabel];'? – Malloc 2012-07-13 07:53:21

1

您將indexPath.row直接與您的模型索引關聯時,它們應該偏移「標題」單元格。

if (indexPath.row && (indexPath.row - 1) < cellList.count) { 
      [cell addSubview:[cellList objectAtIndex:indexPath.row - 1]]; 
} else { 
+0

Thanx作爲回覆。不要擔心變量狀態的'FALSE'檢查,我已經檢查過它,當它'FALSE'時,UILabel根本不顯示,並且一切正常。我的問題仍然處於變量的「TRUE」狀態。 – Malloc 2012-07-13 07:48:15

+0

謝謝,我編輯了我的答案,以消除噪音 – 2012-07-13 07:53:39

1

我會用這兩種細胞的標識符,一個用於LabelCell,一個用於常規ArrayCell,這種情況搞清楚了U,和u不會得到一個標籤和對象的單元格。

另外我真的不知道你在做什麼,但它看起來像你添加subViews到一個單元格每次,但你不要刪除它們在任何地方。不要忘了,細胞得到重用...

1

嗨在我看來,你檢查標籤條件,然後你添加相同的對象在mutablearray因爲你的indexPath.row == 0 < cell.count for第一行。

cell.tag = [self isNoScoreLabelDisplayed]?[indexPath row]+1:[indexPath row];//here i wanted to shift the tags in case the condition is true, so that all the elements will be displayed from the second cell. But seems not doing what i want :(

所以只需設置單元格標籤等於上面的代碼indexpath.row + 1,如果你要顯示的標籤,但下面的代碼(記住,第一次indexPath.row == 0,因此即使如果顯示的標籤)添加相同的數組對象:-)

if (indexPath.row < cellList.count)