ios - 我正在尝试在选择行时向 tableview 单元格中的 UIStackView 添加对象和删除对象 - 它只工作一次

标签 ios objective-c uitableview uistackview

所以我尝试使用委托(delegate) -didSelectRowAtIndexPath 将 UIDatePicker 添加到 UITableView 单元格内的 UIStackView。当我选择行并取消选择该行时,它只起作用一次,不会再次重复该操作。

Stack View 位于 tableview 单元格内并包含一个标签。 日期选择器位于场景停靠栏中。

在 -didSelectRowAtIndexPath 方法中,我正在检查堆栈 View 是否包含日期选择器,如果不包含则添加它,否则将其删除。它只工作一次。

代码如下:

 #import "MyTableViewController.h"

@interface MyTableViewController ()
@property (strong, nonatomic) IBOutlet UIDatePicker *extraPicker;
@property (nonatomic, strong) UIStackView *stackView; //tag 100

@end

@implementation MyTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.estimatedRowHeight = 44;
    self.tableView.rowHeight = UITableViewAutomaticDimension;

}

#pragma addPickerViewToStackView

-(void)addPickerViewToStackView{

    [_stackView addArrangedSubview:_extraPicker];
}

#pragma mark - remove picker
-(void)removePickerFromStackView{

    [UIView animateWithDuration:0.4
                     animations:^{ [_extraPicker setAlpha:0];
                         [_extraPicker.widthAnchor constraintEqualToConstant:self.view.frame.size.width].active = true;
                         [_stackView removeArrangedSubview:_extraPicker];
                         [self.tableView layoutIfNeeded];
                     }];
}




#pragma mark - picker date changed
-(IBAction)pickerValeChanged:(id)sender{
    [self.tableView reloadData];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 3;
    }else if (section == 1){
        return 1;
    }
    return 0;

}


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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"otherCell" forIndexPath:indexPath];
    cell.textLabel.text = @"Starts";

    if (indexPath.row == 2) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pickerCell" forIndexPath:indexPath];
        _stackView = (UIStackView*) [cell viewWithTag:100];
        return cell;
    }

    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [self.tableView beginUpdates];
    [_extraPicker setAlpha:1];

    if (indexPath.row == 1) {
        if ([_extraPicker isDescendantOfView:_stackView]) {
            [self removePickerFromStackView];
        }else if (![_extraPicker isDescendantOfView:_stackView]){
        [self addPickerViewToStackView];
        }

    }


    [self.tableView endUpdates];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}


@end

最佳答案

在阅读了 Apple 网站上的 UIStackView 文档后,我了解到以下重要信息:

This method removes the provided view from the stack’s arrangedSubviews array. The view’s position and size will no longer be managed by the stack view. However, this method does not remove the provided view from the stack’s subviews array; therefore, the view is still displayed as part of the view hierarchy.

To prevent the view from appearing on screen after calling the stack’s removeArrangedSubview: method, explicitly remove the view from the subviews array by calling the view’s removeFromSuperview method, or set the view’s hidden property to YES.

所以我的错误是没有从 superView 中删除 _extraPicker。在 -removePickerFromStackView 中添加此代码后,一切正常。

[_extraPicker removeFromSuperview];

关于ios - 我正在尝试在选择行时向 tableview 单元格中的 UIStackView 添加对象和删除对象 - 它只工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33892019/

相关文章:

objective-c - 如何在 Kiwi 中 stub 方法 block ?

objective-c - UIView touchesbegan 在动画期间没有响应

ios - 如何将两个参数传给下一个ViewController?

ios - Swift - 缓慢的 UITableView 滚动(从设备存储加载图像)

iphone - Connect() 从 ios 到同一子网中的 ios 设备

ios - 为什么通行证后面的App无法加载

ios - 最好的方法是什么,停用约束或更改优先级?

ios - 如何使用 UIPageViewController 跳转到特定页面?

objective-c - AVAssetReader/Writer 只适用于某些设备?

swift - 相同颜色的 TableView 和 TableView 单元格 swift