好的,
我搜索了这个问题的解决方案并尝试了许多不同的东西,但仍然没有运气。
基本上,我有一个包含标签的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];
}
}
答案 0 :(得分:1)
在探测之后,我发现[tableView reloadRowsAtIndexPaths:withRowAnimation]
方法确实调用cellForRowAtIndexPath
方法,该方法总是使用标准值重新绘制单元格。我继续创建一个BOOL
来告诉它何时使用标准值以及何时在tableView的数据源方法中使用操作表中的选定值,它就像魅力一样。