您好,登錄后才能下訂單哦!
代碼拾遺錄--輕松獲取Cell里的button的indexPath
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier from:(UITableView*)table
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.tableView = table;
[self createUI];
}
return self;
}
- (void)createUI
{
self.backgroundColor = [UIColor purpleColor];
self.textLabel.font = [UIFont systemFontOfSize:50.0f];
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = (CGRect){0,0,60,40};
button.backgroundColor = [UIColor whiteColor];
[button addTarget:self action:@selector(clickAction:event:) forControlEvents:UIControlEventTouchUpInside];
self.accessoryView = button;
}
- (void)clickAction:(UIButton*)sender event:(id)event
{
NSSet * touches = [event allTouches];
UITouch * touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint:currentTouchPosition];
if (indexPath != nil) {
NSLog(@"%@",indexPath);
}
}
關鍵代碼 要將table傳入cell里面 還有一句 indexPathForRowAtPoint:
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。