2012-09-03 43 views
2

動態創建嘗試各種選項後,和SO閱讀無數的答案,我張貼這個問題,希望有人能提供一些見解或者對這個問題的解決方案:的UIButton顯示上多次的UITableView時的UITableViewCell

iOS5的。 1,XCode 4.4.1,故事板,ARC

我有一個「分組」的tableview,每個單元格包含一堆標籤,圖像和按鈕。其中一個標籤,說明標籤,因爲它的文本可以是大或小,這意味着如果文本很大,我必須相應地增加標籤的大小,並將相應的按鈕放置在相應的位置。我能夠做到這一切,但問題是,當我滾動瀏覽第一個單元格並在滾動時不斷重複時,按鈕會出現兩次。

這裏是代碼,我正在做的是根據文本計算標籤高度,調整標籤大小,然後相應地將3個按鈕放置在描述標籤下方。

下面的代碼只是一個按鈕,但這應該給我的想法。我試圖在自定義單元類中的「willDisplayCell」中做類似的事情,但仍然重複着3個按鈕,當我在tableview中向下滾動時。請參閱屏幕截圖,前三個按鈕不應該顯示。

我注意到的第一3點的按鈕,位置相同,如果它忽略sizeIncrement,意思是「359.0f + sizeIncrement 20」減去「sizeIncrement」,「sizeIncrement」計算後=說明標籤的高度

我也注意到,如果我這樣做

float y = 359.0f + 20; 

,而不是這個

float y = 359.0f + sizeIncrement +20; 

的repet按鈕問題已經消失。 P:我知道這不是最好的代碼,但我一直在嘗試很多東西來解決這個問題,所以在這一點上不用最佳實踐等等。

2 Cells displayed on screenshot, first 3 buttons shouldNOT show

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

static NSString *CellIdentifier = @"tipsid"; 

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

id tip = [_tips objectAtIndex: [indexPath section]]; 

NSDictionary *tipForIndex = [_tips objectAtIndex: [indexPath section]]; 
float sizeIncrement = [Utility getLabelHeightForText:[tipForIndex objectForKey:@"tipDescripion"] 
              withWidth:285.0f 
              withFont:[UIFont systemFontOfSize:14.0f]]; 

// Here I Resize the Label, code below to create a button and position accordingly. 

float y = 359.0f + sizeIncrement +20; 

UIButton *likeButton = [[UIButton alloc] initWithFrame:CGRectMake(10, y, 80, 26)]; 
likeButton.tag = [indexPath section];  

// add targets and actions 
[likeButton addTarget:self action:@selector(likebuttonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
[likeButton setBackgroundImage:[UIImage imageNamed:@"likebutton.png"] forState:UIControlStateNormal]; 

[likeButton setImage:[UIImage imageNamed:@"likedButton.png"] forState:UIControlStateSelected]; 

// add to a view 
[cell.contentView addSubview:likeButton]; 

.....同樣的方式創建其他2個按鈕

回答

1

你應該移到按鈕創建爲if (cell == nil)操作:

if (cell == nil) 
{ 
    cell = [[TipsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

    // like button 
    float y = 359.0f + sizeIncrement +20; 

    UIButton *likeButton = [[UIButton alloc] initWithFrame:CGRectMake(10, y, 80, 26)]; 
    likeButton.tag = LIKE_BUTTON_TAG_UNIQUE_IN_CONTENT_VIEW; // <<<<<<<<<<----------------  

    // add targets and actions 
    [likeButton addTarget:self action:@selector(likebuttonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
    [likeButton setBackgroundImage:[UIImage imageNamed:@"likebutton.png"] forState:UIControlStateNormal]; 

    [likeButton setImage:[UIImage imageNamed:@"likedButton.png"] forState:UIControlStateSelected]; 

    // add to a view 
    [cell.contentView addSubview:likeButton]; 

    // other buttons 
} 

// and here you can adjust button positions 
UIButton *likeButton = [self.contentView viewWithIndex:LIKE_BUTTON_TAG_UNIQUE_IN_CONTENT_VIEW]; 
+0

Nekto,感謝您的回答。我現在沒有我的開發機器,所以還沒有嘗試過你的建議,但我有一個問題,通常cell == nil返回false,因爲下面的代碼重用已經實例化的單元格「[tableView dequeueReusableCellWithIdentifier:CellIdentifier] ;「,那麼如果條件有幫助,將如何移動代碼? –

+0

爲什麼你需要創建新的按鈕,如果你正在重複使用已經有這些按鈕的單元格? – Nekto

+0

我不確定如果我完全理解你的問題,但在設計模式(Storyboard)中沒有單元格中的按鈕,我需要動態創建按鈕並將它們添加到單元格中的描述標籤下方,如代碼所示。我嘗試了你的建議,但就像我提到的那樣,如果條件使得按鈕從不創建,它永遠不會進入內部。 –

相關問題