2013-06-20 164 views
0

我有一個帶有自定義單元格的表格,在單元格中有兩個UIViews,最上面的一個總是顯示,而最下面的一個隱藏。表格單元格不總是在取消隱藏後顯示隱藏內容

cellCellForRowAtIndexPath被調用時,單元格的高度都降低到只顯示頂部的UIView。

當用戶單擊單元格時,該單元格將展開,然後取消隱藏第二個UIView。問題是,有時第二個UIView不會在第一次單擊單元格時出現。

再次單擊該單元格使其全部消失,然後再次單擊並始終顯示完美。向下滾動,然後選擇另一個,然後第一次點擊它可能不會出現或將出現錯誤的地方,兩個舔,這是完美的。

我認爲這是一個ReusableCell問題,但我不知道如何解決它。

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

cellID = @"Cell"; 

customCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath]; 

// Test to see if the Cell has already been built 
if (cell == nil) 
{ 
    cell = [[customCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]; 
} 

// Check if the selected Cell is being drawn and unhide the pull down view and adjust y origin 
if(indexPath.row == selectedCell) 
{ 
    // Unhide the view 
    cell.pullDownView.hidden = NO; 

    CGRect tempFrame = cell.pullDownView.frame; 
    tempFrame.origin.y = [[secondViewY objectAtIndex:indexPath.row] floatValue]; 
    cell.pullDownView.frame = tempFrame; 

    // Image in second view  
    cell.mainImage2.image = [theImages objectAtIndex:indexPath.row]; 
} 
else 
{ 
    cell.pullDownView.hidden = YES; 
} 

// Image in first view 
cell.mainImage.image = [theImages objectAtIndex:indexPath.row]; 




return cell; 
} 

所以單擊單元格調用didSelectRow它看起來像這樣

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

if(selectedCell == indexPath.row) 
{ 
    selectedCell = -1; 
    [tableView deselectRowAtIndexPath:indexPath animated:NO]; 
} 
else 
{ 
    selectedCell = indexPath.row; 
} 

// [tableView reloadData]; 

[tableView beginUpdates]; 
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationMiddle]; 
[tableView endUpdates]; 
} 

我曾經嘗試都reloadData和BeginUpdates,你可以看到,但這仍然無法正常工作。這是因爲我隱藏/取消隱藏我應該做Alpha到0,然後選擇1?這是一個可重用的單元格事物嗎?

爲了完整性哦。你可能會注意到我有一張桌子,所有的高度都不同,這就是爲什麼我改變第二個UIView的Y的原因。

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

float returnHeightValue; 

if(indexPath.row == selectedCell) 
{ 
    returnHeightValue = [[selectedCellHeights objectAtIndex:indexPath.row] floatValue]; 
} 
else 
{ 
    returnHeightValue = [[normalCellHeights objectAtIndex:indexPath.row] floatValue]; 
} 


return returnHeightValue; 

} 
+0

請注意,圖像是在視圖中,因爲我想添加一個標籤的負載在那裏,我不想隱藏/取消一大堆東西。 –

+0

使用Alpha也不起作用,結果相同。 –

+0

我禁用Y座標,它工作正常,總是在那裏。當然,問題是我需要移動它!我是否錯誤地更改Y座標? –

回答

0

好吧,改變它一切隱藏和取消隱藏更改單元格上的內容不能很好地工作(有遺留在表中的工件,有時甚至不會顯示)。

我現在正在創建兩個自定義單元格,其中一個單元格具有所有其他對象,而另一個單元格沒有選中。

到目前爲止似乎是更好的方法。

0

好吧我改變了我如何處理項目。我已經移除了UIView(它們都嵌入在其中),而現在我已經在我的自定義單元的.h中。

@property (nonatomic, strong) IBOutletCollection(UILabel) NSArray *pullDownItems; 
@property (weak, nonatomic) IBOutlet UIImageView *mainImage; 

不是

@property (weak, nonatomic) IBOutlet UIView *pullDownView; 

好了,所以現在我要取消隱藏和移動兩個項目,而不是一個但這是遠遠高於鼎它們分別會是一個痛苦(有大約10個UILabels更好)。我列舉了這樣的數組;

for(UILabel *obj in cell.pullDownItems) 
    { 
     obj.hidden = NO; 
     tempFrame = obj.frame; 
     tempFrame.origin.y = tempFrame.origin.y - tempYOffset; 
     obj.frame = tempFrame; 
    } 

我剛剛附加的任何新項目。

+0

我應該說我也沒有使用自動佈局,這可能會爲我做所有這些。 –

相關問題