首先,在这里反省一下自己,在遇到问题的时候没有及时去解决,这一点很不好,得改
//尝试获得可以复用的单元格。但不一定获得到。得不到返回nil
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"ft"];
//如果没有获得到
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ft"];
}
[self.tableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"ft"];
MyTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ft" forIndexPath:indexPath];
建立MyTableViewCell文件继承于UITableViewCell
// MyTableViewCell.h文件
@interface MyTableViewCell : UITableViewCell
@property UILabel *label;
@property UIButton *btn;
@end
//MyTableViewCell.m文件
#import "MyTableViewCell.h"
@implementation MyTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if ([self.reuseIdentifier isEqualToString:@"ft"]) {
_label = [[UILabel alloc] init];
[self.contentView addSubview:_label];
_btn = [UIButton buttonWithType:UIButtonTypeCustom];
[self.contentView addSubview:_btn];
}
return self;
}
//布局
- (void)layoutSubviews {
_label.frame = CGRectMake(30, 10, 100, 50);
_btn.frame = CGRectMake(100, 10, 50, 50);
}
@end
在ViewController.h 中添加协议 定义数据视图对象
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
<
//实现数据视图的普通协议
//数据视图的普通事件处理
UITableViewDelegate,
//实现数据视图的数据代理协议
//处理数据视图的数据代理
UITableViewDataSource
>
//定义数据视图对象。数据视图用来显示大量相同格式的大量信息的视图
@property UITableView *tableView;
@end
ViewController.m文件:
#import "ViewController.h"
#import "MyTableViewCell.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
[self.view addSubview:self.tableView];
//设置代理
self.tableView.delegate = self;
self.tableView.dataSource = self;
//注册 使用- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;必须注册
[self.tableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"ft"];
}
//组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
//组内行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
//单元格高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 70;
}
//设置单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ft" forIndexPath:indexPath];
//MyTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ft"];
//if(cell == nil) {
// cell = [[MyTableViewCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:@"ft"];
//}
//以上为UITableView 重用cell两种方法
if(indexPath.section == 0) {
cell.label.text = @"ft";
[cell.btn setImage:[UIImage imageNamed:@"3.jpg"] forState:UIControlStateNormal];
} else {
cell.label.text = @"ff";
[cell.btn setImage:[UIImage imageNamed:@"5.jpg"] forState:UIControlStateNormal];
}
return cell;
}
@end
运行:
本文地址:https://blog.csdn.net/qq_45836906/article/details/107455365
如对本文有疑问, 点击进行留言回复!!
Codeforces Round #657 (Div. 2) C题
hdu2222 Keywords Search(ac自动机模板-kuangbin)
IOS开发控件视图day08:监听文本框状态设置按钮状态不可点击且变灰
网友评论