自定义UITableViewCell中Accessory(已更新)

目前项目中需要用到自定义Cell及cell中的Acessory图片,期间碰到了一些不吐不快的槽,非常抱歉由于项目要赶进度,我只能每周挤点时间出来写写,让大家久等了,sorry。

代码:

    // 设置Accessory
    UIImage *imageAccessory = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"icon_right" ofType:@"png"]];
    UIButton *btnAccessory = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect frame = CGRectMake(0.0f, 0.0f, imageAccessory.size.width, imageAccessory.size.height);
    btnAccessory.frame = frame;
    [btnAccessory setBackgroundImage:imageAccessory forState:UIControlStateNormal];
    [btnAccessory setBackgroundImage:imageAccessory forState:UIControlStateHighlighted];
    btnAccessory.backgroundColor = [UIColor clearColor];
    [btnAccessory addTarget:self action:@selector(btnClicked:event:) forControlEvents:UIControlEventTouchUpInside];
    NSLog(@"btnAccessory = %@", btnAccessory);
    cell.accessoryView = btnAccessory;
    NSLog(@"cell.accessoryView = %@", cell.accessoryView);

#pragma mark - IBAction Methods
// 检查用户点击按钮时的位置,并转发事件到对应的accessory tapped事件
- (void)btnClicked:(id)sender event:(id)event
{
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:tableViewCustom];
    NSIndexPath *indexPath = [tableViewCustom indexPathForRowAtPoint:currentTouchPosition];
    // or instead with such below
    // UITableViewCell *cell = (UITableViewCell *)sender.superview;
    if(indexPath != nil)
    {
        [self tableView:tableViewCustom accessoryButtonTappedForRowWithIndexPath:indexPath];
    }
}


槽点1:

UIImage图片位置必须正确,一次手误打错路径,浪费了2分钟在上面,建议nslog一下以免产生坑爹的空值。


槽点2:

- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath

关于UITableViewCellAccessoryType建议大家每一种都试一下,看看效果如何,但如果要自定义Accessory,这个方法基本上没啥鸟用。

用了以后还有可能迷惑你=。=


槽点3:

自定义Accessory时直接使用以下代码是没用的

accessoryButtonTappedForRowWithIndexPath:indexPath

所以需要增加一个SEL

[btnAccessory addTarget:self action:@selector(btnClicked:event:) forControlEvents:UIControlEventTouchUpInside];
个人觉得可以再UITableViewCellAccessoryType中再增加一个自定义类型,然后直接映射到这个方法,省得麻烦啊~
accessoryButtonTappedForRowWithIndexPath:indexPath

代码我会在周日前附上,先去吃饭了=。=


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值