如何在按钮单击时更改自定义单元格的高度?

时间:2016-07-04 13:54:20

标签: ios iphone cell

我有关于自定义单元格高度的问题。单击单元格上的一个按钮时,我需要增加单元格的高度。她知道使用两种方法(heightforrow和didselectrow),但我很困惑,当我点击按钮时,调用按钮操作的自定义方法,我在控制器中使用这两种方法。我附上了我的代码:

in 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?

}

任何人都可以帮助我吗? 感谢

4 个答案:

答案 0 :(得分:2)

在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;

}

答案 1 :(得分:1)

尝试添加布尔变量作为类成员。单击按钮时设置它。 重新加载- (IBAction)btnRestPlusClicked:(id)sender

末尾的表格视图

如果设置了布尔变量,则在- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath中更改tableview单元格的高度。

答案 2 :(得分:1)

问题不是heightForRowAtIndexPath:的实现,而是让运行时再次调用 heightForRowAtIndexPath:,以便它可以发现您的单元格高度已经发生变化。为此,您的按钮应该将表视图告诉reloadData

答案 3 :(得分: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];
}