今天遇到这样的需求,让cell 在点击的时候 附加在cell上面的button不要高亮显示,在网上查了一下,解决这种问题的方法有这么几种
1.
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
[super setHighlighted:highlighted animated:animated];
if(highlighted) {
[(UIButton *)self.accessoryView setHighlighted:NO];
}
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated{
[super setSelected:selected animated:animated];
if(selected) {
[(UIButton *)self.accessoryView setHighlighted:NO];
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[supertouchesBegan:touches withEvent:event];
[(UIButton *)self.accessoryView setHighlighted:NO];
}
3.还有最后一个用系统,修改方法
-(void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
[cell.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj isKindOfClass:[UIButton class]]) {
[obj setHighlighted:NO];
}
}];
}
-(void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
[cell.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj isKindOfClass:[UIButton class]]) {
[obj setHighlighted:NO];
}
}];
}
double delayInSeconds = 0.00001;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[obj setHighlighted:NO];
});
整个是这样的
-(void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
for (id obj in cell.subviews ) {
if ([obj isMemberOfClass:[UIButton class]]){
[obj setHighlighted:NO];
double delayInSeconds = 0.00001;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[obj setHighlighted:NO];
});
}
}
}
相信这样应该可以解决掉点击整个cell的时候 在cell 上加的button不高亮,点击button的时候 button是高亮显示的;