首页 > 其他 > 详细

tableView分割线的indexPath

时间:2015-08-28 17:24:46      阅读:158      评论:0      收藏:0      [点我收藏+]

在写项目时候,tableView的分割线一般都会自定义,我采取的方法是为UITableView额外的添加一个叫

 1 - (BOOL)tableView:(UITableView *)tableView borderForRowAtIndexPath:(NSIndexPath *)indexPath; 

的协议方法,说起这个协议方法,我一般都是在搭建项目框架时候,都会写一个tableView的基类,也会自定义一个cell的基类,在基类中制定一个继承于

UITableViewDataSource, UITableViewDelegate的协议,然后在子类中重载tableView的协议方法,而上面的borderForRowAtIndex:就是这个协议的协议方法,

在tableView的基类.h文件中代码:

 1 @protocol CardViewDelegate;
 2 
 3 @interface CardView : UITableView
 4 
 5 @property (nonatomic, assign)id<CardViewDelegate>cardViewDelegate;
 6 
 7 @end
 8 
 9 @protocol CardViewDelegate <UITableViewDataSource, UITableViewDelegate>
10 
11 @optional
12 - (BOOL)tableView:(UITableView *)tableView borderForRowAtIndexPath:(NSIndexPath *)indexPath;
13 
14 @end

这个borderForRowAtIndex:方法的实现:

 1 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 2 {
 3     UITableViewCell *cell = nil;
 4     if (self.cardViewDelegate && [self.cardViewDelegate respondsToSelector:@selector(tableView:cellForRowAtIndexPath:)]) {
 5         cell = [self.cardViewDelegate tableView:tableView cellForRowAtIndexPath:indexPath];
 6     }
 7     if ([cell isKindOfClass:[CardCell class]]) {
 8         CardCell *cardCell = (CardCell *)cell;
 9         BOOL border = YES;
10         if (self.cardViewDelegate && [self.cardViewDelegate respondsToSelector:@selector(tableView:borderForRowAtIndexPath:)]) {
11             border = [self.cardViewDelegate tableView:tableView borderForRowAtIndexPath:indexPath];
12         }
13         cardCell.borders = border;
14     }
15     
16     return cell;
17 }

在自定义cell基类中,实现如下代码:

 1 - (void)drawRect:(CGRect)rect {
 2     CGContextRef context = UIGraphicsGetCurrentContext();
 3     if (self.borders == YES) {
 4         
 5         CGContextMoveToPoint(context, CGRectGetMinX(rect) + 10, CGRectGetMaxY(rect));
 6         CGContextAddLineToPoint(context, CGRectGetMaxX(rect) - 10, CGRectGetMaxY(rect));
 7     }
 8     else if (self.borders == NO){
 9        
10     }
11     CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor] );
12     CGContextSetLineWidth(context, 0.5f);
13     CGContextStrokePath(context);
14 }
15 - (void)setBorders:(BOOL)borders {
16     if (_borders != borders) {
17         _borders = borders;
18         [self setNeedsDisplay];
19     }
20 }

那么在当前的UITableView中,去重载borderForRowAtIndex:方法:

1 - (BOOL)tableView:(UITableView *)tableView borderForRowAtIndexPath:(NSIndexPath *)indexPath {
2     if (indexPath.row == 0) {
3         return YES;
4     }
5     else {
6         return NO;
7     }
8 }

到这里,设置indexPath的值,就可以控制哪个cell的分割线是否出现,其实这种做法是简易的去返回BOLL的YES或NO去实现,那么我在实际项目中不会这么写,因为单单的YES or NO太单一了,我会写成

 1 - (NSInteger)tableView:(UITableView *)tableView borderForRowAtIndexPath:(NSIndexPath *)indexPath; 

其中它返回的值是枚举类型

 1 typedef NS_ENUM(NSInteger, Border) {
 2     TOP = 0x00000001,
 3     LEFT = 0x00000002,
 4     RIGHT = 0x00000004,
 5     BOTTOM = 0x00000008,
 6     
 7     TOP_WITH_MARGIN = 0x00000010,
 8     BOTTOM_WITH_MARGIN = 0x00000020,
 9     
10     NONE = 0x10000000,
11 
12 };

这样的写法无疑可以设置的余地大大增加了

 

tableView分割线的indexPath

原文:http://www.cnblogs.com/03-06-hanwei/p/4766848.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!