【问题标题】:Update UITableViewCell with value from UIAlertController action sheet使用 UIAlertController 操作表中的值更新 UITableViewCell
【发布时间】:2017-05-10 20:19:45
【问题描述】:

好的,

我到处寻找解决方案并尝试了许多不同的方法,但仍然没有运气。

基本上,我有一个包含标签的 tableViewCell。当用户点击标签时,会弹出一个带有一些选项的操作表。做出选择后,我想更新用户点击以启动操作表的相同标签。这可能吗?

我在下面发布了代码。使用 NSLog 语句,我能够确定标签的值在操作表操作的完成处理程序中正在更改,但是一旦执行该代码块,标签的值就会重置为其原始值,就像最初从未发生过变化。我还尝试设置我的委托以从操作表返回字符串值,但找不到任何内置方法可用于操作表,如 iOS 8 之前的版本。非常感谢任何帮助。

Parent.h文件:

#import <UIKit/UIKit.h>

@interface TableViewController : UITableViewController

@end

Parent.m 文件的相关部分:

@interface EPSTableViewController () <MyACPresentationDelegate>

@end

@implementation EPSTableViewController

#pragma mark - Custom delegate methods

-(void)presentActionSheet {
    // Init the roof type action sheet
    UIAlertController *actionSheetRoofType = [UIAlertController alertControllerWithTitle:@"Roof Type"
                                                                             message:@"Please select a roof type."
                                                                      preferredStyle:UIAlertControllerStyleActionSheet];

    [actionSheetRoofType addAction:[UIAlertAction actionWithTitle:@"Asphalt"
                                                        style:UIAlertActionStyleDefault
                                                      handler:^(UIAlertAction * _Nonnull action) {
                                                          dispatch_async(dispatch_get_main_queue(), ^{
                                                              NSArray *arrayIndexPaths = [self.tableView indexPathsForVisibleRows];
                                                              NSIndexPath *cellIndexPath = [arrayIndexPaths objectAtIndex:9];

                                                              EPSTableViewCell *roofTypeCell =  (EPSTableViewCell *)[self tableView:self.tableView cellForRowAtIndexPath:cellIndexPath];

                                                              NSLog(@"Roof type is: %@", roofTypeCell.labelRoofTypeSelection.text);

                                                              roofTypeCell.labelRoofTypeSelection.text = @"Asphalt";
                                                              [self.tableView reloadRowsAtIndexPaths:@[cellIndexPath] withRowAnimation:UITableViewRowAnimationNone];

                                                              NSLog(@"Roof type is now: %@", @"Asphalt");                                                              });
                                                      }]];

    [self presentViewController:actionSheetRoofType animated:YES completion:nil];

    NSLog(@"%@ was selected", selection);
}

Child.h

@protocol MyACPresentationDelegate <NSObject>

-(void)presentActionSheet;

@end

@interface EPSTableViewCell : UITableViewCell

@property (nonatomic, strong) UILabel *labelRoofType;
@property (nonatomic, strong) UILabel *labelRoofTypeSelection;
@property (nonatomic, weak) id <MyACPresentationDelegate> presentationDelegate;

@end

Child.m

-(void)configureCell {
    // Init the roof type label
    self.labelRoofType = [[UILabel alloc] init];

    // Setup the average usage label
    self.labelRoofType.userInteractionEnabled = NO;
    self.labelRoofType.text = @"Roof Type:";
    self.labelRoofType.adjustsFontSizeToFitWidth = YES;
    self.labelRoofType.translatesAutoresizingMaskIntoConstraints = NO;
    self.labelRoofType.hidden = NO;
    self.labelRoofType.numberOfLines = 1;
    self.labelRoofType.textColor = [UIColor blackColor];
    self.labelRoofType.textAlignment = NSTextAlignmentLeft;

    // Init the roof type selection label (to be used with action sheet)
    self.labelRoofTypeSelection = [[UILabel alloc] init];

    // Setup the average usage label
    self.labelRoofTypeSelection.userInteractionEnabled = YES;
    self.labelRoofTypeSelection.text = @"Tap for roof type";
    self.labelRoofTypeSelection.adjustsFontSizeToFitWidth = YES;
            self.labelRoofTypeSelection.translatesAutoresizingMaskIntoConstraints = NO;
    self.labelRoofTypeSelection.hidden = NO;
    self.labelRoofTypeSelection.numberOfLines = 1;
    self.labelRoofTypeSelection.textColor = [UIColor blackColor];
    self.labelRoofTypeSelection.textAlignment = NSTextAlignmentLeft;

    // Init and setup tap gesture recognizer for the roof type selection label
    UITapGestureRecognizer *tgrRoofTypeSelection = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                               action:@selector(labelRoofTypeSelectionTapped)];
    tgrRoofTypeSelection.numberOfTapsRequired = 1;

    // Add tap gesture recognizer to label
    [self.labelRoofTypeSelection addGestureRecognizer:tgrRoofTypeSelection];

    // Add views to cell
    [self addSubview:self.labelRoofType];
    [self addSubview:self.labelRoofTypeSelection];

    // Add view constraints
    NSDictionary *views = [NSDictionary dictionaryWithObjects:@[self.labelRoofType, self.labelRoofTypeSelection]
                                                          forKeys:@[@"label", @"selection"]];

    NSLayoutConstraint *labelConstraint = [NSLayoutConstraint constraintWithItem:self.labelRoofType
                                                                           attribute:NSLayoutAttributeCenterY
                                                                           relatedBy:NSLayoutRelationEqual
                                                                              toItem:self
                                                                           attribute:NSLayoutAttributeCenterY
                                                                          multiplier:1
                                                                            constant:0];

    NSArray *arrayConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[label]-(>=8,<=100)-[selection]"
                                                                            options:NSLayoutFormatAlignAllCenterY
                                                                            metrics:nil
                                                                              views:views];

    [self addConstraint:labelConstraint];
    [self addConstraints:arrayConstraints];

}

-(void)labelRoofTypeSelectionTapped {
    if ([self.presentationDelegate respondsToSelector:@selector(presentActionSheet)]) {
        [self.presentationDelegate presentActionSheet];
    }
}

【问题讨论】:

  • 您可能想要更新用作UITableView 数据源的模型。首先为你的模型中的标签设置新值,然后调用[myTableView reloadData];
  • 感谢您的建议。经过一番探索,我发现[tableView reloadRowsAtIndexPaths:withRowAnimation] 方法确实调用了cellForRowAtIndexPath 方法,它总是用标准值重绘单元格。我继续创建了一个 BOOL 来告诉它何时使用标准值以及何时使用 tableView 的数据源方法中的操作表中的选定值,它就像一个魅力。非常感谢的人!
  • 很高兴能帮上忙!

标签: ios objective-c uitableview uialertcontroller


【解决方案1】:

经过一番探索,我发现[tableView reloadRowsAtIndexPaths:withRowAnimation] 方法确实调用了cellForRowAtIndexPath 方法,它总是用标准值重绘单元格。我继续创建了一个 BOOL 来告诉它何时使用标准值以及何时使用 tableView 的数据源方法中的操作表中的选定值,它就像一个魅力。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-09
    • 1970-01-01
    相关资源
    最近更新 更多