[英]UITableViewCell custom accessory button
我在TableView中的单元格中使用此附件:
cell.accessoryType = UITableViewCellAccessoryDetailButton;
我希望这个细节按钮看起来像文本字段中的清除按钮。 我该怎么做,仍然能够使用accessoryButtonTappedForRowWithIndexPath:indexPath
方法。
唯一的方法是将UITableViewCell子类化。 让它成为CustomTableViewCell。 然后编写协议CustomTableViewCell,其中define方法
@protocol CustomTableViewCellDelegate
- (void)customButtonTappedForRowWithIndexPath:(NSIndexPath*)indexPath;
@end
然后在YourTableViewController中定义委托和实现协议
#import "CustomTableViewCell.h"
@interface YourTableViewController : UITableViewController <CustomTableViewCellDelegate>
@property (nonatomic, weak) id<CustomTableViewCellDelegate> delegate;
..............................
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ChatTableViewCellIdentifier forIndexPath:indexPath];
cell.delegate = self;
cell.indexPath = indexPath;
..........
}
- (void)customButtonTappedForRowWithIndexPath:(NSIndexPath*)indexPath
{
<YOUR_CODE_FOR_PRESS_HANDLER>
}
CustomTableViewCell描述:
@protocol CustomTableViewCellDelegate;
@interface CustomTableViewCell : UITableViewCell
@property (nonatomic, strong) NSIndexPath *indexPath;
@end
...................
@implementation CustomTableViewCell
- (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
UIButton *yourButton = [UIButton buttonWithType:UIButtonTypeCustom];
yourButton.frame = CGRectMake(0, 0, 25, 25);
[yourButton.frame setBackgroundImage:[UIImage imageNamed:@"clear_image_name"] forState:UIControlStateNormal];
[yourButton.frame setBackgroundImage:[UIImage imageNamed:@"clear_image_name_pressed"] forState:UIControlStateHighlighted];
self.accessoryView = yourButton;
[yourButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
return self;
}
- (void) buttonPressed:(id)sender
{
[self.delegate customButtonTappedForRowWithIndexPath:self.indexPath];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.