您的位置:首页 > 产品设计 > UI/UE

UITableView与UIScrollView的一些问题(持续更新)

2014-04-17 10:19 393 查看
UITableView的一些常用操作

--------------------------------------------------------------------------------------------

// 隐藏TableView中cell之间的分割线

tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

// 隔行显示不同的颜色

if (indexPath.row % 2 == 1)

{

cell.backgroundColor = ......;

}

else

{

cell.backgroundColor = ......;

}

--------------------------------------------------------------------------------------------

1. 一个View中要处理几个TableView时怎么办?

使用TableView的tag值做标记,来区分不同的TableView,然后在代理函数中用tag值区分.

typedef enum {

APP_TABLE_VIEW = 0x11,

SIGN_TABLE_VIEW,

} EFlag;

UITableView *appTableView = ...;

appTableView.tag = APP_TABLE_VIEW;

UITableView *signTableView = ...;

signTableView.tag = SIGN_TABLE_VIEW;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

if (tableView.tag == APP_TABLE_VIEW)

{

// 分离出appTableView

}

if (tableView.tag == SIGN_TABLE_VIEW)

{

// 分离出signTableView

}

return nil;

}

2. 如何让选中的某个cell后快速的恢复到选中前的状态(选中时的灰色动态消失)?

本人用GCD的延时程序处理,当然可以使用performSelector:withObject:afterDelay:来执行延时操作,读者可以自己试试,很麻烦,很明显,能用简单的为啥还要用复杂的呢......

#pragma mark - 延时多少毫秒

- (void)delayTime:(int64_t)microSeconds inQueue:(dispatch_queue_t)queue

block:(void (^)(dispatch_queue_t queue))block

{

dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, microSeconds * USEC_PER_SEC);

dispatch_after(popTime, queue, ^(void){

block(queue);

});

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

// 延时100ms后执行deselectRowAtIndexPath动画消失,注意,实在主线程中执行,别在后台线程执行

[self delayTime:100 inQueue:dispatch_get_main_queue() block:^(dispatch_queue_t queue) {

[tableView deselectRowAtIndexPath:indexPath animated:YES];

}];

}

3. 自定义的cell中如果存在着UIButton,那如何将点击事件传递出去呢?

自定义cell后,将indexPath参数传递到自定义的cell中(NSIndexPath为cell的一个retain的属性),自定义的cell申明一个协议,里面提供了一个方法

- (void)touchEventAtIndexPath:(NSIndexPath *)indexPath;

......

@property (nonatomic, assign) id<AppTableViewCellDelegate>delegate; // 将事件传递出去

......

- (void)buttonsEvent:(UIButton *)button

{

// 将点击事件传递出去

[self.delegate touchEventAtIndexPath:_indexPath];

}

当然,也可以用block实现,以后补上

4. contentOffset在动画中设定了新值并执行了动画操作,在协议方法中scrollViewDidScroll:会执行多次吗?

这是在动画中来设置contentOffset的值来达到移动UIScrollView的效果

[UIView animateWithDuration:duration animations:^{

CGPoint point = _mainScrollView.contentOffset;

point.x = 0;

_mainScrollView.contentOffset = point;

}];

这是协议自身方法移动UIScrollView后会调用的方法

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

  ......

}

经过测试,改变contentOffset的值并执行动画后,scrollViewDidScroll:仅执行一次,无论contentOffset的值改变了多少.

5. 自定义cell中能够设置自己的高度吗?如果不能设置高度,那怎么给cell中的各种视图布局?

高度是通过tableView:heightForRowAtIndexPath:方法来设置的,实际上,自定义cell的布局不受高度影响,只存在高度太低而遮盖住了要显示的内容而已.

6. 如何在指定的row上显示自定义的cell并能完整的重用呢?

如下例,在指定row为2的前提下,其步骤与正常使用是一致的,只是需要设置一个新的重用标示而已.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

if (indexPath.row == 2)

{

static NSString *my = @"my";

MyTableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:my];

if (myCell == nil)

{

myCell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault

reuseIdentifier:my];

}

return myCell;

}

else

{

static NSString *str = @"normal";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];

if (cell == nil)

{

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault

reuseIdentifier:str];

}

return cell;

}

}

7. 一个UIScrollview中包含了几个UITableView,而某个UITableView中的自定义cell中包含了一个UIScrollview,左右滑动的手势切换这几个UITableView时,当手触发在这个cell时,其UIScrollview会得到响应吗?

这个cell的UIScrollview会得到响应,与最外层的UIScrollview互不影响.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: