一、UITableview的使用
UITableViewDataSource @required
1.多少组数据:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
2.多少行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
3.设置cell的每组每行
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
2、通过代码方式自定义cell
(1)新建⼀一个继承自UITableViewCell的类
(2)重写initWithStyle:reuseIdentifier:方法
MessageCell.h
#import@interface MessageCell : UITableViewCell@property (retain, nonatomic) UIImageView *headImageView;@end
MessageCell.m
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { self.selectionStyle = UITableViewCellSelectionStyleNone; self.backgroundColor = [UIColor colorWithHex:0xefefef]; UIView *ticketKuang = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWith, cellHeight)]; [self.contentView addSubview:ticketKuang]; ticketKuang.backgroundColor = [UIColor whiteColor]; //选择按钮 selectBtn = [[UIButton alloc]initWithFrame:CGRectMake(-44, 5, 44, 44)]; [selectBtn setImage:[UIImage imageNamed:@"icon_list_selectBox_normal"] forState:UIControlStateNormal]; [selectBtn setImageEdgeInsets:UIEdgeInsetsMake(12, 12, 12, 12)]; //{top, left, bottom, right} [ticketKuang addSubview:selectBtn]; return self; }}
/****使用系统或者自定义cell*****/// static NSString *cellIdentify =@"cellIdentify";// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];// if(cell == nil)// { UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; cell.selectionStyle = UITableViewCellSelectionStyleNone;// }