Chr*_*iet 2 objective-c uitableview ios
我有一个UITableViewController自定义单元格,我用自己的子类定制.在这个子类中,我添加了一个按钮,我想将视图推入导航控制器的堆栈中.我不知道如何做到这一点,因为我不知道如何从我的自定义单元格访问导航控制器.
任何的想法?
这里需要更多信息.什么类保存表,tableview委托是什么类?
在最简单的情况下,你在一个单独的班级工作.比它会[self.navigationController pushViewController: xyz].
但是如果你有自己的子类UITableViewCells,那么你需要在单元类和viewcontroller之间进行通信.您可以通过在单元类或您自己的customCell委托中设置属性来完成此操作.
你也可以发送一个[[NSNotificationCenter defaultCenter] postNotification: @"cellButtonTouchedNotification"]你的viewController正在监听的Notification()([[NSNotificationCenter defaultCenter] addListener: self target: @selector(...) name: @"cellButtonTouchedNotification"]).在这种情况下,您可以使用userInfo属性来记住触摸了哪个单元格.
另一方面是使按钮可从外部访问(例如属性).然后你可以在tableViewDelegate的方法中添加目标cellForRowAtIndexPath:.水木清华.喜欢[myCustomCell.button addTarget: self selector: @selector(...)];你可以使用标签来识别行myCustomCell.button.tag = indexPath.row.
使用委托.这是一个简单的例子.
//.h
@protocol MyTableViewCellDelegate;
@interface MyTableViewCell : UITableViewCell
@property (assign, nonatomic) id <MyTableViewCellDelegate> delegate;
//your code here
@end
@protocol MyTableViewCellDelegate <NSObject>
@optional
- (void)delegateForCell:(MyTableViewCell *)cell;
@end
//.m
@implementation MyTableViewCell
@synthesize delegate = _delegate;
- (void)prepareForReuse {
[super prepareForReuse];
self.delegate = nil;
}
- (void)buttonAction {
if ([self.delegate respondsToSelector:@selector(delegateForCell:)])
[self.delegate delegateForCell:self];
}
@end
Run Code Online (Sandbox Code Playgroud)
单击该按钮时,会向您的单元格的委托发送消息(例如,插入导航控制器的表视图控制器).
在这里控制器
@implementation YourController
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *reuseIdentifier = @"MyCustomCell";
MyTableViewCell *cell = (id)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (cell == nil)
cell = [[[MyTableViewCell alloc] initWithMyArgument:someArgument reuseIdentifier:reuseIdentifier] autorelease];
[cell setDelegate:self];
// update your cell
return cell;
}
- (void)delegateForCell:(MyTableViewCell *)cell {
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
// do your stuff
[self.navigationController pushViewController:...];
}
@end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4957 次 |
| 最近记录: |