2016-07-04 69 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]; 
}