Ios 如何克服UITableViewCell子类的限制
我有一个自定义的Ios 如何克服UITableViewCell子类的限制,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个自定义的uitableviewcell和子类,它包含一个uitextfield,并且还设置了委托,现在当按下键盘上的返回键时,我想尝试一些事情 执行一个segue(但问题是我在uitableviewcell子类中) 以模式显示另一个视图控制器(但问题是uitableviewcell) 不允许这样做) 我想显示uiactionsheet(但限制仍然存在) uitableviewcell) 如果我得到rootviewcontroller引用,则rootviewcontroller的视图本身不
uitableviewcell
和子类
,它包含一个uitextfield
,并且还设置了委托
,现在当按下键盘上的返回键
时,我想尝试一些事情
如果我得到rootviewcontroller引用,则rootviewcontroller的视图本身不显示或不是活动视图,因此您所做的任何事情都不会出现在屏幕上,需要活动视图 您可以在单元格上使用块属性,该属性在自定义按钮操作发生时被激发。单元格的块属性可能如下所示:
@interface CustomTableViewCell : UITableViewCell
@property (nonatomic, copy) void (^customActionBlock)();
@end
@implementation CustomTableViewCell
- (IBAction)buttonTapped:(id)sender {
if ( self.customActionBlock ) {
self.customActionBlock();
}
}
@end
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"customCell" forIndexPath:indexPath];
cell.textLabel.text = [self.colors objectAtIndex:indexPath.row];
cell.customActionBlock = ^{
NSLog(@"Do the stuff!");
// present view controller modally
// present an action sheet
// etc....
};
return cell;
}
然后,您的单元格将从自定义按钮操作调用此块,如下所示:
@interface CustomTableViewCell : UITableViewCell
@property (nonatomic, copy) void (^customActionBlock)();
@end
@implementation CustomTableViewCell
- (IBAction)buttonTapped:(id)sender {
if ( self.customActionBlock ) {
self.customActionBlock();
}
}
@end
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"customCell" forIndexPath:indexPath];
cell.textLabel.text = [self.colors objectAtIndex:indexPath.row];
cell.customActionBlock = ^{
NSLog(@"Do the stuff!");
// present view controller modally
// present an action sheet
// etc....
};
return cell;
}
最后,将-cellforrowatinexpath:
中的块设置回视图控制器(或任何位置),如下所示:
@interface CustomTableViewCell : UITableViewCell
@property (nonatomic, copy) void (^customActionBlock)();
@end
@implementation CustomTableViewCell
- (IBAction)buttonTapped:(id)sender {
if ( self.customActionBlock ) {
self.customActionBlock();
}
}
@end
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"customCell" forIndexPath:indexPath];
cell.textLabel.text = [self.colors objectAtIndex:indexPath.row];
cell.customActionBlock = ^{
NSLog(@"Do the stuff!");
// present view controller modally
// present an action sheet
// etc....
};
return cell;
}
不过,有一点要注意。如果使用块,则会有强烈引用self
并造成内存泄漏的风险。积木很有趣,也很容易使用,但你必须遵守它们的规则。以下是一些帮助您熟悉它们的资源:
@interface CustomTableViewCell : UITableViewCell
@property (nonatomic, copy) void (^customActionBlock)();
@end
@implementation CustomTableViewCell
- (IBAction)buttonTapped:(id)sender {
if ( self.customActionBlock ) {
self.customActionBlock();
}
}
@end
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"customCell" forIndexPath:indexPath];
cell.textLabel.text = [self.colors objectAtIndex:indexPath.row];
cell.customActionBlock = ^{
NSLog(@"Do the stuff!");
// present view controller modally
// present an action sheet
// etc....
};
return cell;
}
然后,您的单元格将从自定义按钮操作调用此块,如下所示:
@interface CustomTableViewCell : UITableViewCell
@property (nonatomic, copy) void (^customActionBlock)();
@end
@implementation CustomTableViewCell
- (IBAction)buttonTapped:(id)sender {
if ( self.customActionBlock ) {
self.customActionBlock();
}
}
@end
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"customCell" forIndexPath:indexPath];
cell.textLabel.text = [self.colors objectAtIndex:indexPath.row];
cell.customActionBlock = ^{
NSLog(@"Do the stuff!");
// present view controller modally
// present an action sheet
// etc....
};
return cell;
}
最后,将-cellforrowatinexpath:
中的块设置回视图控制器(或任何位置),如下所示:
@interface CustomTableViewCell : UITableViewCell
@property (nonatomic, copy) void (^customActionBlock)();
@end
@implementation CustomTableViewCell
- (IBAction)buttonTapped:(id)sender {
if ( self.customActionBlock ) {
self.customActionBlock();
}
}
@end
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"customCell" forIndexPath:indexPath];
cell.textLabel.text = [self.colors objectAtIndex:indexPath.row];
cell.customActionBlock = ^{
NSLog(@"Do the stuff!");
// present view controller modally
// present an action sheet
// etc....
};
return cell;
}
不过,有一点要注意。如果使用块,则会有强烈引用self
并造成内存泄漏的风险。积木很有趣,也很容易使用,但你必须遵守它们的规则。以下是一些帮助您熟悉它们的资源:
表格视图中,也可以将操作附加到按钮上
[cell.button addTarget:self action:@selector(presentController:) forControlEvents:UIControlEventTouchUpInside];
presentController
指的是iAction
- (IBAction)presentController:(id)sender
{
//present
}
即使按钮位于表格视图中,也可以将操作附加到按钮上
[cell.button addTarget:self action:@selector(presentController:) forControlEvents:UIControlEventTouchUpInside];
presentController
指的是iAction
- (IBAction)presentController:(id)sender
{
//present
}
在Tableview超类中实现按钮操作。
也可以在UITableViewCell子类中使用自定义委托。在UITableView子类中声明一个协议
@protocol customCellDelegate <NSObject>
@required
-(void)selectedButtonInIndexPath : (NSIndexPath *)indexpath;
@end
在tableview数据源方法中
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
实现此代码
cell.delegate = (id)self;
cell.indexpath = indexPath;
在Uitableview超类中,只需实现此方法
-(void)selectedButtonInIndexPath : (NSIndexPath *)indexpath{
//display uiimagepickercontroller modally or anything else here
}
在Tableview超类中实现按钮操作。
也可以在UITableViewCell子类中使用自定义委托。在UITableView子类中声明一个协议
@protocol customCellDelegate <NSObject>
@required
-(void)selectedButtonInIndexPath : (NSIndexPath *)indexpath;
@end
在tableview数据源方法中
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
实现此代码
cell.delegate = (id)self;
cell.indexpath = indexPath;
在Uitableview超类中,只需实现此方法
-(void)selectedButtonInIndexPath : (NSIndexPath *)indexpath{
//display uiimagepickercontroller modally or anything else here
}
您总是可以从窗口的rootViewController以模式显示VC,对吗?问题是您无法连接ui按钮
操作,或者您不知道如何从表视图单元格以模式显示另一个视图控制器?-1因为问题尚未澄清感谢澄清您的问题。不幸的是,这和你最初问的有点不同。此外,根据您对当前未显示的窗口的rootViewController
的评论,这意味着您可能已经在它上面显示了其他视图控制器。无论如何,这不是UITableViewCell
的限制,只是它的工作方式@Bhaskar与一名代表合作的方法是可行的。您还可以在表视图单元格上使用块,以便在单击单元格按钮时允许表或其视图执行某些操作。您可以始终从窗口的rootViewController以模式显示VC,对吗?问题是您无法连接UIButton
操作,或者您不知道如何从“表视图”单元格以模式显示另一个视图控制器?-1,因为问题尚未澄清感谢您澄清问题。不幸的是,这和你最初问的有点不同。此外,根据您对当前未显示的窗口的rootViewController
的评论,这意味着您可能已经在它上面显示了其他视图控制器。无论如何,这不是UITableViewCell
的限制,只是它的工作方式@Bhaskar与一名代表合作的方法是可行的。您还可以在“表视图”单元格上使用块,以便在单击单元格的按钮时允许表或其视图执行某些操作。感谢您的回复,创建委托不是一种好方法吗?有几种方法,但我正在尝试采用标准方法。标准方法是使用xcode拖放,但由于单元格是动态的,如果我声明一个委托通知uitableviewcontroller,显示uiactionsheet或以模式显示viewcontroller,则需要按我的回答中所示的方式以编程方式执行。那么是吗?问题是什么?请再次查看问题,我已更新它。谢谢。谢谢您的回复,创建代表不是一个好方法吗?有几种方法,但我正在尝试采用标准方法。标准方法是使用xcode拖放,但由于单元格是动态的,如果我声明一个委托通知uitableviewcontroller,显示uiactionsheet或以模式显示viewcontroller,则需要按我的回答中所示的方式以编程方式执行。那么是吗?问题是什么?请再次查看问题,我已更新它。谢谢。请阅读上面的评论,我已经知道这些方法,但我正在寻找一种标准的方法。没有标准的方法来解决你的问题