定义自己的tablecell的方式总结,相关使用TableVIew的教程很多,下面说说我在使用过程中的一些经验,希望对大家有帮助
#import "ViewController.h"
#import "TableViewCell.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
BOOL type;
int number;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return number;
}
static NSString *cellIdf1 = @"cell1";
static NSString *cellIdf2 = @"cell2";
static BOOL nibsRegistered = NO;
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
/** 代码方式创建 */
/**
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdf1];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdf1];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(cell.frame), CGRectGetHeight(cell.frame))];
[title setTag:200];
[cell addSubview:cell];
}
UILabel *titleLb = (UILabel *)[cell viewWithTag:200];
[titleLb setText:@"test"];
*/
/** xib方式 */
/**
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdf1];
if (cell == nil) {
//testCellXib 为uitableviewcell 的xib文件名称
cell = [[[NSBundle mainBundle] loadNibNamed:@"testCellXib" owner:nil options:nil] lastObject];
}
UILabel *titleLb = (UILabel *)[cell viewWithTag:300];
[titleLb setText:@"xibCellTest"];
*/
/** UITableViewCell的子类结合xib的方式 */
/**
static NSString *CustomCellIdentifier = @"CustomCellIdentifier";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
if (cell == nil) {
NSArray *xib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];
cell = [xib lastObject];
}
*/
/** 使用继承UITableViewCell的子类CustomCell */
/**
static NSString *CustomCellIdentifier = @"CustomCellIdentifier";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CustomCellIdentifier];
}
*/
// 通过注册的方式 先注册有使用 参考 注册的部分可以放到 viewDidLoad中 推荐使用有利于cell重复使用,以上的方法在cell中处理的数据太多时滚动不流畅
// - (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(5_0);
// - (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
/** 注册一个 */
/**
static NSString *CustomCellIdentifier = @"CustomCellIdentifier";
static BOOL nibsRegistered = NO;
if (!nibsRegistered) {
[tableView registerNib:[UINib nibWithNibName:@"CustomCellXib" bundle:nil] forCellReuseIdentifier:CustomCellIdentifier];
nibsRegistered = YES;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
*/
// 注册不同的列表样式
if (!nibsRegistered) {
if (indexPath.row % 2 == 0) {
type = 0;
[_mTableView registerNib:[UINib nibWithNibName:@"cell1" bundle:nil] forCellReuseIdentifier:cellIdf1];
}else{
type = 1;
[_mTableView registerClass:[TableViewCell class] forCellReuseIdentifier:cellIdf2];
}
nibsRegistered = YES;
}
UITableViewCell *cell;
if (type == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:cellIdf1];
[cell setBackgroundColor:[UIColor redColor]];
[[cell textLabel] setText:@"celll1"];
}
if (type == 1) {
cell = [tableView dequeueReusableCellWithIdentifier:cellIdf2];
[cell setBackgroundColor:[UIColor greenColor]];
[[cell textLabel] setText:@"cell2"];
}
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)segAction:(UISegmentedControl *)sender {
number = 10;
switch (sender.selectedSegmentIndex) {
case 0:
type = 0;
[_mTableView registerNib:[UINib nibWithNibName:@"cell1" bundle:nil] forCellReuseIdentifier:cellIdf1];
break;
case 1:
type = 1;
[_mTableView registerClass:[TableViewCell class] forCellReuseIdentifier:cellIdf2];
break;
default:
break;
}
[_mTableView reloadData];
}
@end
IOS UITableView 使用总结
最新推荐文章于 2024-07-07 03:25:16 发布