2014-10-07 57 views
-1

我現在有在該按鈕時每個部分的viewForFooterInSection一個按鈕,從0至65的高度的高度增加的行5-10的高度,如下所示:如何在UITableView的每個部分有不同的行高?

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { 
    UIView *view = [[UIView alloc] initWithFrame:CGRectZero]; 
    view.backgroundColor = [UIColor whiteColor]; 

    self.moreButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    self.moreButton.frame = CGRectMake(0, 0, 320, 44); 
    [self.moreButton setImage:[UIImage imageNamed:@"downarrow.png"] forState:UIControlStateNormal]; 
    [self.moreButton addTarget:self action:@selector(moreButtonSelected:) forControlEvents:UIControlEventTouchUpInside]; 
    [view addSubview:self.moreButton]; 

    return view; 
} 

- (void)moreButtonSelected:(id)sender { 
    if (_hasPressedShowMoreButton == NO){ 
     self.hasPressedShowMoreButton = YES; 
    } 
    else if (_hasPressedShowMoreButton == YES){ 
     self.hasPressedShowMoreButton = NO; 
    } 

    [self.matchCenter reloadData]; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (_hasPressedShowMoreButton == YES){ 
     return 65; 
    } 

    else if (_hasPressedShowMoreButton == NO){ 
     if (indexPath.row > 3){ 
      return 0; 
     } 
     else{ 
      return 65; 
     } 
    } 

} 

這就意味着是「顯示更多」類型的動作,這樣當按下時,其餘行將出現,而不是僅前4個。問題是,當按下某個節的頁腳中的按鈕時,它會展開所有部分到10行,而不僅僅是按下按鈕的那一行。我怎樣才能指定按鈕被按下的部分?

編輯:

我試過以下的建議,並做如下圖所示,但它引發的崩潰與錯誤'NSInvalidArgumentException', reason: '-[UIButton section]: unrecognized selector sent to instance 0x7f94840640b0'

代碼:

- (void)moreButtonSelected:(NSIndexPath *)indexPath{ 
    if (_hasPressedShowMoreButton == NO){ 
     self.hasPressedShowMoreButton = YES; 
     indexPath.section == _expandedSection; 
    } 
    else if (_hasPressedShowMoreButton == YES){ 
     self.hasPressedShowMoreButton = NO; 
    } 

    [self.matchCenter reloadData]; 
} 


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (_hasPressedShowMoreButton == YES){ 
     if (indexPath.section == _expandedSection){ 
      return 65; 
     } 

    } 

    else if (_hasPressedShowMoreButton == NO){ 
     if (indexPath.row > 3){ 
      return 0; 
     } 
     else{ 
      return 65; 
     } 
    } 

} 
+0

您不能使用單個法爾'_hasPressedShowMoreButton'。您需要跟蹤所有部分的按鈕狀態。 – rmaddy 2014-10-07 17:30:59

+0

@rmaddy啊,有道理。介紹如何跟蹤每個按鈕的狀態? – Ghobs 2014-10-07 17:33:34

回答

1

您應該檢查indexPath.section == _expandedSection。 (將您按下的更多按鈕的indexPath.section存儲到_expandedSection中。)

擴展UIButton以允許分配給它。

@interface MoreButton : UIButton 
@property (assign) NSInteger sectionIndex; 
@end 

@implementation MoreButton 
@end 

將屬性添加到您的班級。

@property (assign) NSInteger expandedSection; 
self.expandedSection = -1; 

當您創建它(不使用self.創建它),分配sectionIndex

MoreButton *moreButton = [MoreButton buttonWithType:UIButtonTypeCustom]; 
moreButton.sectionIndex = section; 
[moreButton addTarget:self action:@selector(moreButtonSelected:) forControlEvents:UIControlEventTouchUpInside]; 
[view addSubview:moreButton]; 

然後在您的處理程序:

- (void)moreButtonSelected:(MoreButton *)button { 
    self.expandedSection = button.sectionIndex; 
    [self.matchCenter reloadData]; 
} 

而且你的身高爲行:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (indexPath.section == self.expandedSection || indexPath.row <= 3) { 
    return 65; 
    } 
    return 0; 
} 

您可以使用您的expandedSectionsNSMutableSet,而是如果你想擴展多個在同一時間,使用這些行:

@property (strong) NSMutableSet *expandedSections; 
self.expandedSections = [NSMutableSet set]; 

if ([self.expandedSections containsObject:@(section)]) { 
    [self.expandedSections removeObject:@(section)]; 
} else { 
    [self.expandedSections addObject:@(section)]; 
} 

if ([self.expandedSections containsObject:@(indexPath.section)] || indexPath.row <= 3) 
+0

啊有道理。不過,我有點困惑,「將更多按鈕的'indexPath.section'存儲到'_expandedSection'中意味着什麼。向我展示一個例子?謝謝! – Ghobs 2014-10-10 18:47:22

+0

爲我更新了我的答案。 – 2014-10-10 19:17:30

+0

它給我下面的錯誤:'對於架構x86_64的未定義符號: 「_OBJC_CLASS _ $ _ MoreButton」,從引用: objc級,裁判在MatchCenterViewController.o LD:符號(S)沒有發現建築x86_64的 鐺:錯誤:鏈接器命令失敗,退出代碼1(使用-v查看調用) ' – Ghobs 2014-10-10 20:34:11

1

我想的問題是,indexPath.row並不總是使用 - 當_hasPressedShowMoreButton是真實的,它無論返回65:

if (_hasPressedShowMoreButton == YES){ 
    return 65; 
} else if (_hasPressedShowMoreButton == NO){ 
    if (indexPath.row > 3){ 
     return 0; 
    } 
    else{ 
     return 65; 
    } 
} 

你需要改變它的東西更多在以下結構中,判斷indexPath.row與最外面的如果:

if (indexPath.row > 3) { 
    //some logic here 
} else { 
    //some logic 
} 
+0

請檢查我的編輯。 – Ghobs 2014-10-10 19:12:35

相關問題