您的位置:首页 > 其它

项目遇到的问题汇总<一>之下拉菜单所有问题

2016-03-24 18:54 387 查看
这一个月被项目给整的,都没时间写博客了,今天忙里偷闲,总结一下这段时间项目中自己遇到的卡到自己的问题,我昨天负责实现一个下拉菜单,然后写了四个大的View,每个view中又有几个小的View,先说这个下拉菜单,其实挺简单,但是按照产品的要求,还是卡了我一会的

1.当我们在设置一个button的文字和图片的时候,左边显示文字,右边显示图片,(这个下拉控件就是左边文字,右边下拉图片),我是这么做的,设置button的文字,然后设置button的图片,setImage,这样出现的问题就是,文字在右,图片在左,肯定不可以,于是我设置文字和图片之间的偏移量:

CGFloat labelWidth = button.titleLabel.bounds.size.width;

CGFloat imageWidth = button.imageView.bounds.size.width;

button.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth, 0, -labelWidth);

button.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWidth, 0, imageWidth);解决问题了,可是,总感觉效果不好,于是有了另外一种思路,button上,左边放一个label,右边放一个imageView,OK 解决。下拉菜单出来后,里边的文字个数不是统一的,但是我在选择某个的时候是需要让那个下拉按钮跟着变化的,这样就必须自适应这个下拉按钮的宽了,所以还得用到自适应,

//title字数自适应 image自适应

CGRect rect = [titleName boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:PB_FONT_4} context:nil];

lable.frame = CGRectMake((centerBtn.frame.size.width-rect.size.width)/2, lable.frame.origin.y, rect.size.width, lable.frame.size.height);

lable.text = titleName;

UIImageView *imageView = (UIImageView *)[centerBtn viewWithTag:View_TAG5];

imageView.frame = CGRectMake(lable.frame.origin.x+rect.size.width, lable.frame.origin.y, imageView.frame.size.width, imageView.frame.size.height);

接着当你点中这个button的时候,需要在下拉table上显示我当前显示的那个button的标示,字体变蓝,右边还有一个对号以示选中,这样的话,

UIImageView *selectImg = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width - 15-WIDTH_RIGHT_MARGIN-90, HEIGHT_TOP_MARGIN+2, 10, 10)];

selectImg.image = [UIImage imageNamed:@"hq_Select.png"];

if (indexPath.row == mSelectIndexRow) {

cell.textLabel.textColor = PB_COLOR_1;

selectImg.hidden = NO;

}else {

selectImg.hidden = YES;

}

其中mSelectIndexRow需要在didSelectRowAtIndexPath这个代理方法中记录下来,OK,解决!

2.当我们在设置tableView的headTitle的时候,需要调用 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section,但是只调用这个的话,设置头视图是设置不了的,因为不会调用这个方法,还需要调用这个方法 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

return 33;

}

这样才可以设置我们我们需要的头视图。

3.添加移除手势,当点击button,出来这个table后,需求肯定不是不点击这个下拉table就不能操作,所以,引入了手势,至于这个手势,我也是有话说的,我是这么实现的,当点击下拉button的时候会出现一个下拉table,我把这个 table不是放在当前view上,而是放在了一个backgroundView上,backgroundView再放在当前view上,而现在这个backgroundVIew上增加手势,手势是这样的,

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeClick:)];

tap.delegate = self;

[mBackgroundView addGestureRecognizer:tap];

同时实现手势代理的两个方法,因为,我不是整个view添加手势,而是给backgroundView除了那个下拉table的别的面积添加手势,所以。。。下边

#pragma mark -UIGestureRecognizerDelegate

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

return YES;

}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

{

if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {

return NO;

}

return YES;

}

而同时手势实现方法是我把backgroundVIew和table从当前view上移除掉,当点击下拉按钮的时候,重新创建下拉table和backgroundView。》》》》OK,解决!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: