11.简化表格单元格的生成
PRPDoubleRainbowCell *cell = [PRPDoubleRainbowCell
@implementation PRPSmartTableViewCell
+ (NSString *)cellIdentifier {
}
+ (id)cellForTableView:(UITableView *)tableView {
}
- (id)initWithCellIdentifier:(NSString *)cellID {
}
12. 在NIB中使用智能表格单元格
拓展11中的方法,并使用nib文件来配置单元格:
+ (NSString *)cellIdentifier {
}
+ (id)cellForTableView:(UITableView *)tableView fromNib:(UINib *)nib {
}
#pragma mark -
#pragma mark Nib support
+ (UINib *)nib {
}
+ (NSString *)nibName {
}
使用:
PRPComplexTableViewCell是带有nib文件的basecell的子类,把其nib文件设置成了一个属性
- (UINib *)complexCellNib {
}
这里要注意
1.默认xib名和类名和identifier名一样
2.使用UINIB类加载nib可以提高效率,此时要使用对应方法:[nib
- (IBAction)cellButtonTapped:(id)sender {
}
14.组织复杂的表格视图
当表格视图结构复杂时,最好用枚举数来表示表格的section和row,这样做的好处很多:易于维护修改(这点十分明显和重要),结构清楚明了。(注意,枚举数多加一个表示总数目也是一个技巧)
enum PRPTableSections {
};
enum PRPFavoritesRows {
};
enum PRPAlertsRows {
};
15. 生成双色(背景)表格视图
重载layoutSubviews的方法
- (void)layoutSubviews {
}
16.给表格视图添加边框阴影
17,在滚动视图中使用静态内容
即在scrollview里放一个子视图,这个视图要求在父视图缩放的时候位置大小都不变。
(若添加为独立的视图,其相对scorollview位置会变,若添加到scrollview大小又会变)
解决办法是:缩放时会自动调用setTransform,再这个方法中使用adjustSubviewsForTransfo
- (void)setTransform:(CGAffineTransform)transform {
}
- (void)adjustSubviewsForTransfo
}
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];
}
return
cell;
}
+ (NSString *)cellIndentifier{
return
NSStringFromClass([self
class
]);
}
- (id)initWithCellIdentifier:(NSString *)cellID{
return
[self initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
@end
|
下面来探讨在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
|
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{
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:0] isKindOfClass:[self
class
]], @
"Nib '%@' does not appear to contain a valid %@"
, [self nibName], NSStringFromClass([self
class
]));
cell = [nibObjects objectAtIndex:0];
}
return
cell;
}
#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
|
[[NSBundle mainBundle] loadNibNamed:@"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"
;
return
cell;
}
|