简化表格单元格的生成++++++

来源:互联网 发布:花开花落知多少下一句 编辑:程序博客网 时间:2024/06/05 19:14



http://blog.sina.com.cn/s/blog_6b60259a0101bbkc.html

11.简化表格单元格的生成

使用单元格复用的时候需要写大量重复的代码,违反DO NOT REPEAT YOURSELF的原则,重复的东西就可以复用。
创建一个基类完成这样的任务,其他cell可以子类化这个类并重载
- (id)initWithCellIdentifier:(NSString *)cellID {  方法即可。使用的时候只需要

PRPDoubleRainbowCell *cell = [PRPDoubleRainbowCell 

                                  cellForTableView:tableView];一句话就可以了,配置则cell.XXXlabel.text=```



@implementation PRPSmartTableViewCell


+ (NSString *)cellIdentifier {

    return NSStringFromClass([self class]);

}


+ (id)cellForTableView:(UITableView *)tableView {

    NSString *cellID = [self cellIdentifier];

    UITableViewCell *cell = [tableView 

                             dequeueReusableCellWithIdentifier:cellID];

    if (cell == nil) {

        cell = [[[self allocinitWithCellIdentifier:cellID] autorelease];

    }

    return cell;    

}



- (id)initWithCellIdentifier:(NSString *)cellID {

    return [self initWithStyle:UITableViewCellStyleSubtitle 

               reuseIdentifier:cellID];

}



12. 在NIB中使用智能表格单元格

拓展11中的方法,并使用nib文件来配置单元格:

 

+ (NSString *)cellIdentifier {

    return NSStringFromClass([self class]);

}


+ (id)cellForTableView:(UITableView *)tableView fromNib:(UINib *)nib {

    NSString *cellID = [self cellIdentifier];

    UITableViewCell *cell = [tableView 

                             dequeueReusableCellWithIdentifier:cellID];

    if (cell == nil) {

        NSArray *nibObjects = [nib instantiateWithOwner:nil options:nil];

 

        NSAssert2(([nibObjects count] > 0) && 

                  [[nibObjects objectAtIndex:0isKindOfClass:[self class]],

                  @"Nib '%@' does not appear to contain a valid %@"

                  [self nibName], NSStringFromClass([self class]));

 

        cell = [nibObjects objectAtIndex:0];

    }

    return cell;    

}


#pragma mark -

#pragma mark Nib support


+ (UINib *)nib {

    NSBundle *classBundle = [NSBundle bundleForClass:[self class]];

    return [UINib nibWithNibName:[self nibNamebundle:classBundle];

}


+ (NSString *)nibName {

    return [self cellIdentifier];

}


使用:

 

  PRPComplexTableViewCell *cell =

        [PRPComplexTableViewCell cellForTableView:tableView 

                                          fromNib:self.complexCellNib];


PRPComplexTableViewCell是带有nib文件的basecell的子类,把其nib文件设置成了一个属性


 

- (UINib *)complexCellNib {

    if (complexCellNib == nil) {

        self.complexCellNib = [PRPComplexTableViewCell nib];

    }

    return complexCellNib;    

}


这里要注意

1.默认xib名和类名和identifier名一样

2.使用UINIB类加载nib可以提高效率,此时要使用对应方法:[nib instantiateWithOwner:nil options:nil];

而不是 nsbundle loadnibname的方法


13.定位单元格子视图
经常需要找到单元格中控件的位置,比如找到单元格中的按钮所在的位置,将按钮与tableview的信息对应起来,一个比较好而且强壮的方法是使用空间所在的位置构建这种对应关系。

- (IBAction)cellButtonTapped:(id)sender {

    UIButton *button = sender;

    CGPoint correctedPoint = 

      [button convertPoint:button.bounds.origin toView:self.tableView];

    NSIndexPath *indexPath = 

      [self.tableView indexPathForRowAtPoint:correctedPoint];

     `````

}



14.组织复杂的表格视图

当表格视图结构复杂时,最好用枚举数来表示表格的section和row,这样做的好处很多:易于维护修改(这点十分明显和重要),结构清楚明了。(注意,枚举数多加一个表示总数目也是一个技巧)

enum PRPTableSections {

    PRPTableSectionFavorites = 0,

    PRPTableSectionAlerts,

    PRPTableNumSections,

};


enum PRPFavoritesRows {

    PRPTableSecFavoritesRowTeam = 0,

    PRPTableSecFavoritesRowColor,

    PRPTableSecFavoritesRowCity,

    PRPTableSecFavoritesNumRows,

};

enum PRPAlertsRows {

    PRPTableSecAlertsRowAlerts = 0,

    PRPTableSecAlertsNumRows,

};



15. 生成双色(背景)表格视图

重载layoutSubviews的方法

 

- (void)layoutSubviews {

    [super layoutSubviews];

    if (self.topStretcher) {

        if (self.contentOffset.y > 0) {

            self.topStretcher.hidden = YES;        

        } else {

            self.topStretcher.frame = CGRectMake(0self.contentOffset.y,

                                                 self.frame.size.width,

                                                 -self.contentOffset.y);

            self.topStretcher.hidden = NO;

        }

    }

    

    CGFloat contentBottom = (self.contentSize.height - self.contentOffset.y);

    CGFloat bottomGap = self.frame.size.height - contentBottom;

    if ((bottomGap > 0) && self.bottomStretcher) {

        if (self.contentOffset.y < 0) {

            self.bottomStretcher.hidden = YES;        

        } else {

            self.bottomStretcher.frame = CGRectMake(0self.contentSize.height,

                                                    self.frame.size.width,

                                                    bottomGap);

            self.bottomStretcher.hidden = NO;

        } 

    } else {

        self.bottomStretcher.hidden = YES;

    }

}




16.给表格视图添加边框阴影



17,在滚动视图中使用静态内容

即在scrollview里放一个子视图,这个视图要求在父视图缩放的时候位置大小都不变。

(若添加为独立的视图,其相对scorollview位置会变,若添加到scrollview大小又会变)

解决办法是:缩放时会自动调用setTransform,再这个方法中使用adjustSubviewsForTransform对其进行逆变换

 

- (void)setTransform:(CGAffineTransform)transform {

    [super setTransform:transform];

    [self adjustSubviewsForTransform:transform];

    NSLog(@"%f",transform.a);

}


- (void)adjustSubviewsForTransform:(CGAffineTransform)transform {

    CGAffineTransform inversion = CGAffineTransformInvert(transform);

    for (UIView *subview in self.nonScalingSubviews) {

        subview.transform = inversion;

    }

}



18.创建旋转翻页的滚动视图

无限滚动视图。


http://my.oschina.net/orangef/blog/142807?p=1

通过继承UITableViewCell简化表格单元的生成:代码如下

XYCustomCell.h

?
1
2
3
4
5
6
7
8
9
10
11
#import <UIKit/UIKit.h>
 
@interface XYCustomCell : UITableViewCell
 
 
+ (id)cellForTableView:(UITableView *)tableView;
+ (NSString *)cellIndentifier;
 
- (id)initWithCellIdentifier:(NSString *)cellID;
 
@end

XYCustomCell.m

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#import "XYCustomCell.h"
 
@implementation XYCustomCell
 
+ (id)cellForTableView:(UITableView *)tableView{
    NSString *cellID = [self cellIndentifier];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if(cell == nil) {
        cell = [[[self alloc] initWithCellIdentifier:cellID] autorelease];
    }
    returncell;
}
 
+ (NSString *)cellIndentifier{
    returnNSStringFromClass([self class]);
}
 
- (id)initWithCellIdentifier:(NSString *)cellID{
    return[self initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
     
}
 
@end
+cellForTableView:类方法负责处理调用者传入的表格视图的单元格复用。单元格表示福是从另一个类方法+cellIdentifier获取的。注意:如果标识符没有如多数示例代码那样表位static类型,所以在默认的实现中做了一点额外的内存分配。如果觉得这是问题,可覆盖或编辑+cellIdentifier以改变其行为。


下面来探讨在nib中使用智能表格单元格
XYCustomNibCell.h

?
1
2
3
4
5
6
7
8
9
10
11
12
#import <UIKit/UIKit.h>
 
@interface XYCustomNibCell : UITableViewCell
 
+ (NSString *)cellIdentifier;
 
+ (UINib *)nib;
 
+ (NSString *)nibName;
 
+ (id)cellForTableView:(UITableView *)tableView fromNib:(UINib *)nib;
@end
XYCustomNibCell.m 
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#import "XYCustomNibCell.h"
 
@implementation XYCustomNibCell
 
+ (NSString *)cellIdentifier{
    returnNSStringFromClass([self class]);
}
+ (id)cellForTableView:(UITableView *)tableView fromNib:(UINib *)nib{
    NSString *cellID = [self cellIdentifier];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if(cell == nil) {
        NSArray *nibObjects = [nib instantiateWithOwner:nil options:nil];
         
        NSAssert2([nibObjects count] > 0 && [[nibObjects objectAtIndex:0] isKindOfClass:[self class]], @"Nib '%@' does not appear to contain a valid %@", [self nibName], NSStringFromClass([self class]));
        cell = [nibObjects objectAtIndex:0];
    }
     
    returncell;
}
#pragma mark - nib suport
+ (UINib *)nib{
    NSBundle *classBundle = [NSBundle bundleForClass:[self class]];
    return[UINib nibWithNibName:[self nibName] bundle:classBundle];
}
 
+ (NSString *)nibName{
    return[self cellIdentifier];
}
@end
在 + ( id )cellForTableView:( UITableView *)tableView fromNib:( UINib *)nib方法中,我们使用-instantiateWithOwer:options:方法以获得nib对象的一个新副本,而不是调用

[[NSBundle mainBundleloadNibNamed:@"Cell" owner:self options:nil]。

使用策略:首先子类化XYCustomNibCell,创建XIB,使XIB文件名与类名一致,标识符与类名相同,设置XIB的布局,并在子类中添加属性。在使用表格的ViewController中为nib声明一个属性,创建一个懒初始化器,对各种用例实现按需创建nib,最后注意内存管理就行了。部分代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
- (UINib *)complexCellNib{
    if(_complexCellNib == nil) {
        self.complexCellNib = [XYNibBasedCell nib];
    }
    return_complexCellNib;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    XYNibBasedCell *cell = [XYNibBasedCell cellForTableView:tableView fromNib:self.complexCellNib];
    cell.mainLabel.text = @"hahha";
    cell.timeLabel.text = @"time";
    cell.detailDepLabel.text = @"detail";
    returncell;
}



0 0
原创粉丝点击