我想要定制自己的 UITableView,现在我想将 UITableViewCell 拖进 UITableView,可是我怎么样弄,UITableView 就是不接纳 UITableViewCell. 我试着将其它的如 UIImageView 拖进 UITableView 能拖进去。
我捣鼓了半个小时,还没有搞定,谁来指导一下嘛。
自定义的 Cell 是不能直接拖进去的,可以从 nib 中获得 Cell,然后在 TableView 的 cellForRowAtIndexPath 中设置
#1 楼 @bl0ckme 那 他这个怎么可以呢?https://github.com/winfield/ruby-china-ios
用代码的方式实现,我写了个简单的
定义你的 cell 头文件
#import <UIKit/UIKit.h> @interface MyCell : UITableViewCell @end m文件 #import "MyCell.h" @implementation MyCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 150, 25)]; [tempLabel setText:@"Test my Label"]; [self addSubview:tempLabel]; [tempLabel release]; } return self; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } @end
在你的 tableView 初始化 cell 的时候
#pragma mark - #pragma mark Table View Data Source Methods - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [noteListArray count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"thecell"]; if(cell == nil){ cell =[[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"thecell"]; } return cell; }
主要是这句
if(cell == nil){ cell =[[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"thecell"]; }
Sorry,不知道怎么格式化代码和高亮
#3 楼 @welsonla 代码我知道怎么样实现,我现在想用 Interface Builder 对定制的 UITableViewCell 进行布局,那样方便一些,如调整位置,样式等。
#4 楼 @qichunren 选中本来带的 CellView,在 Attribute 里将 Class 修改为自己的 subclass,设定正确的 identifier。
从不用 xib。说实话那套东西拖来拖去,这里设置那设置真的很复杂。 项目多了,把一些常用组件用代码封起来,开发还爽。
if u wanna drag it in, use storyboard