2016-07-04 87 views
0

我有關於自定義單元格高度的問題。當單擊單元格上的一個按鈕時,我需要增加單元格的高度。她知道使用兩種方法(heightforrow和didselectrow),但我很迷惑,當我點擊按鈕時,按鈕動作的自定義方法被調用,我在controller中使用這兩種方法。我附上了我的代碼:如何更改按鈕點擊自定義單元格的高度?

在customcell.m

- (IBAction)btnRestPlusClicked:(id)sender 
{ 
    UIButton *btn = (id)sender; 
    btn.selected =!btn.selected; 
    if (btn.selected) 
    { 
     NSLog(@"selected"); 
     // _viewExtraScheduleAmount.hidden = FALSE;//I need to make this event on button action and same time increase the height of cell. 
    } 
    else 
    { 
     btn.tag = 0; 
     // _viewExtraScheduleAmount.hidden = TRUE; 
    } 

現在我需要的是,當按鈕點擊那個時候只有行的高度也會增加。

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

    //if (ceel.btnRestPlusClicked.selected) 
    //{ 
    // return 100; 
    // } 
    // return 60; 
I know I am wrong here but how to use this method? 

} 

請問任何人可以幫我解決這個問題嗎? 謝謝

回答

1

在UIViewController內部創建NSMutableDictionary,您將在其中存儲btnRestPlusClicked單元格的NSIndexPath。

然後跟蹤按鈕時加在每個單元選擇:

在CustomCell.h

@protocol CustomCellDelegate; 

@interface CustomCell : UITableViewCell  
@property (nonatomic, weak) id<CustomCellDelegate> delegate; 
@end 

@protocol CustomCellDelegate <NSObject> 
- (void)customCellButtonPlusSelected:(CustomCell*)cell; 
@end 

在CustomCell.m

- (IBAction)btnRestPlusClicked:(id)sender 
{ 
    [self.delegate customCellButtonPlusSelected:self]; 
} 

在UIViewController中,當您創建的cellForRowAtIndexPath添加單元格:

cell.delegate = self 

,並符合對的UIViewController CustomCellDelegate

-(void)customCellButtonPlusSelected:(CustomCell*)cell { 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 
    NSString *key = [NSString stringWithFormat:@"%li:%li", indexPath.section, indexPath.row]; 
    [buttonPlusClickedDictionary setObject:@"1" forKey:key]; 
    [self.tableView reloadRowsAtIndexPaths:@[indexPath]]; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *key = [NSString stringWithFormat:@"%li:%li", indexPath.section, indexPath.row]; 
    if ([buttonPlusClickedDictionary objectForKey:key]) { 
     return 100; 
    } 
    return 60; 

} 
+0

謝謝!這是非常有幫助的。它解決了我的問題。再次感謝ruslan.musagitov –

+0

你非常歡迎:) –

0

嘗試添加一個布爾變量作爲類成員。當你點擊按鈕時設置它。 在的末尾重新加載表格視圖

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath如果設置布爾變量,則更改tableview單元格的高度。

0

的問題是不是heightForRowAtIndexPath:但在得到運行時呼叫heightForRowAtIndexPath:再次,以便它可以發現你的高度已經改變了實現。要做到這一點,你的按鈕應該把表格視爲reloadData

0

您可以更改所選單元格的高度時,你對單元格按鈕挖掘像

ViewController.h 

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> 
{ 
    IBOutlet UITableView *myTableView; 
} 
@end 


ViewController.m 
@interface ViewController() 

@end 

@implementation ViewController { 

    NSArray *tableData; 
    int currentselection; 
    int cellno; 
} 

- (void)viewDidLoad { 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    currentselection = -1; 
    tableData = [NSArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", @"Hamburger", @"Ham and Egg Sandwich", @"Creme Brelee", @"White Chocolate Donut", @"Starbucks Coffee", nil]; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [tableData count]; 
} 

- (CGFloat)tableView:(UITableView)tableView heightForRowAtIndexPath:(NSIndexPath)indexPath { 
    int rowHeight; 
    if ([indexPath row] == currentselection) { 
     rowHeight = 200; 
    } else { 
     rowHeight = 50; 
    } 
    return rowHeight; 
} 

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

    static NSString *simpleTableIdentifier = @"SimpleTableItem";  
    NSInteger myInteger = indexPath.row; 
    cellno = (int) myInteger; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; 
    } 
    cell.textLabel.text = [tableData objectAtIndex:indexPath.row]; 

    // add custom expand height button 
    UIButton *addFriendButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    addFriendButton.frame = CGRectMake(200.0f, 5.0f, 75.0f, 30.0f); 
    [addFriendButton setTitle:@"Add" forState:UIControlStateNormal]; 
    [cell addSubview:addFriendButton]; 
    [addFriendButton addTarget:self action:@selector(addHeight:) forControlEvents:UIControlEventTouchUpInside]; 
    [addFriendButton setTag:cellno]; 

    return cell; 
} 

- (IBAction) addHeight:(UIButton *)sender { 

    NSInteger myInteger = sender.tag; 
    currentselection = (int) myInteger; 
    [myTableView beginUpdates]; 
    [myTableView endUpdates]; 
} 
相關問題