很多时候都会用到自定义的uitableviewcell来实现自己的需求。
下面以自定义CustomCell为例子:
MyCell.h文件:
1
2
3
4
5
6
7
|
@interface
MyCell : UITableViewCell
{
UITextView *myTextView;
}
- (
IBAction
)btnAction:(
id
)sender;
@property
(retain,
nonatomic
) IBOutletUITextView *myTextView;
@end
|
MyCell.m文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#import "MyCell.h"
@implementation
MyCell
@synthesize
myTextView;
- (
id
)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(
NSString
*)reuseIdentifier
{
self
= [
super
initWithStyle:style reuseIdentifier:reuseIdentifier];
if
(
self
)
{
}
return
self
;
}
- (
void
)setSelected:(
BOOL
)selected animated:(
BOOL
)animated
{ [
super
setSelected:selected animated:animated];}
- (
IBAction
)btnAction:(
id
)sender {}
|
1
2
3
4
5
|
- (UITableViewCell *)tableView:(UITableView *)tableView
//nib设置了重用标识符,则tableview会使用重用机制
cellForRowAtIndexPath:(
NSIndexPath
*)indexPath
{
static
NSString
*cellid=@
"CellReuseID"
;
MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:cellid];(寻找标识符为cellid并且没被用到的cell用于重用)
|
1
|
if
(cell==
nil
) <br> { <br> cell = [[[
NSBundle
mainBundle] loadNibNamed:@
"MyCell"
owner:
self
options:
nil
] lastObjects]; <br>
//如果此nib没有设置标识符,则当其移出屏幕时会自动释放(dealloc),可以用cell = [MyCell alloc] init];使其不自动释放<br> }<br> NSUInteger row = [indexPath row]; <br> [cell.myTextView setText:@"123456"]; <br> cell.myTextView.editable = NO; <br> return cell;<br> }
|