自定义的UITableViewCell

自定义的UITableViewCell,第1张

概述转自:http://www.cnblogs.com/lovecode/archive/2012/01/07/2315630.html 自定义UITableViewCell的accessory样式       默认的accessoryType属性有四种取值:UITableViewCellAccessoryNone、UITableViewCellAccessoryDisclosureIndicator

转自:http://www.cnblogs.com/lovecode/archive/2012/01/07/2315630.html

自定义UItableVIEwCell的accessory样式
      默认的accessoryType属性有四种取值:UItableVIEwCellAccessoryNone、UItableVIEwCellAccessorydisclosureIndicator、UItableVIEwCellAccessoryDetaildisclosurebutton、UItableVIEwCellAccessorycheckmark。如果想使用自定义附件按钮的其他样式,则需使用UItableVIEw的accessoryVIEw属性来指定。

UIbutton *button;
if(isEditableOrNot) {
UIImage *image = [UIImage imagenamed:@"delete.png"];
button = [UIbutton buttonWithType:UIbuttonTypeCustom];
CGRect frame = CGRectMake(0.0, button.frame = frame;
[button setBackgroundImage:image forState:UIControlStatenormal];
button.backgroundcolor = [UIcolor clearcolor];
cell.accessoryVIEw = button;
}else{
button = [UIbutton buttonWithType:UIbuttonTypeCustom];
button.backgroundcolor = [UIcolor clearcolor];
cell.accessoryVIEw = button;
}

以上代码仅仅是定义了附件按钮两种状态下的样式,问题是现在这个自定义附件按钮的事件仍不可用。即事件还无法传递到UItableVIEwDelegate的accessorybuttonTappedForRowWithIndexPath方法上。当我们在上述代码中在加入以下语句:
             [button addTarget:self action:@selector(btnClicked:event:) forControlEvents:UIControlEventtouchUpInsIDe];
后,虽然可以捕捉到每个附件按钮的点击事件,但我们还无法进行区别到底是哪一行的附件按钮发生了点击动作!因为addTarget:方法最多允许传递两个参数:target和event,这两个参数都有各自的用途了(target指向事件委托对象,event指向所发生的事件)。看来只依靠Cocoa框架已经无法做到了。

      但我们还是可以利用event参数,在自定义的btnClicked方法中判断出事件发生在UItableVIEw的哪一个cell上。因为UItableVIEw有一个很关键的方法indexPathForRowAtPoint,可以根据触摸发生的位置,返回触摸发生在哪一个cell的indexPath。而且通过event对象,正好也可以获得每个触摸在视图中的位置。

// 检查用户点击按钮时的位置,并转发事件到对应的accessory tapped事件
- (voID)btnClicked:(ID)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)
{
[self tableVIEw:self.tableVIEw accessorybuttonTappedForRowWithIndexPath:indexPath];
}
}
这样,UItableVIEw的accessorybuttonTappedForRowWithIndexPath方法会被触发,并且获得一个indexPath参数。通过这个indexPath参数,我们即可区分到底哪一行的附件按钮发生了触摸事件。

- (voID)tableVIEw:(UItableVIEw *)tableVIEw accessorybuttonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
int *IDx = indexPath.row;
这里加入自己的逻辑}
总结

以上是内存溢出为你收集整理的自定义的UITableViewCell全部内容,希望文章能够帮你解决自定义的UITableViewCell所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1055152.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存