#import "TableViewController.h"
@interface TableViewController ()
@property (retain, nonatomic) UITableView *myTable;
@property (assign, nonatomic) NSMutableArray *data;
@property (assign, nonatomic) NSInteger rowNow;
@property (assign, nonatomic) NSInteger sectionNow;
@end
@implementation TableViewController
-(void)loadData{
_rowNow=-1;
_sectionNow=-1;
self.data=[[NSMutableArray alloc] init];
NSArray *fonts=[UIFont familyNames];
NSMutableArray *temp=nil;
for (int i=0; i<fonts.count; i++) {
if (i%5==0) {
temp=[[NSMutableArray alloc] initWithCapacity:5];
[self.data addObject:temp];
[temp release];
}
[temp addObject:fonts[i]];
}
NSLog(@"%@",self.data);
}
-(void)loadTableView{
self.myTable=[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
self.myTable.delegate=self;
self.myTable.dataSource=self;
[self.view addSubview:self.myTable];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellID=@"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
cell.textLabel.text=[[self.data objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
// 这个if else 判断 设定 就能起到 避免这个cell复用 所产生的新cell为选中就由CheckMarm 标记
if (indexPath.row==_rowNow&&indexPath.section==_sectionNow) {
cell.accessoryType=UITableViewCellAccessoryCheckmark;
}else{
cell.accessoryType=UITableViewCellAccessoryNone;
}
return cell;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.data.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [[self.data objectAtIndex:section] count];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
/**
* 通过下面的设置 1,取消上一个row的CheckMark 2,设置当前的row的checkmark 3,重置_rowNow 和_sectionNow
* 可以达到 只让当前选中的row有CheckMark 标识,其它的row没有!
* 但是还是会因为有重用的原因使得 复用的cell 还是未被点击就表上了CheckMark, 这个的解决办法就要在cell生成的时候设置了
*/
// 去掉上一个row的 checkMark
NSIndexPath *lastIndexPath=[NSIndexPath indexPathForRow:_rowNow inSection:_sectionNow];
UITableViewCell *lastCell=[tableView cellForRowAtIndexPath:lastIndexPath];
lastCell.accessoryType=UITableViewCellAccessoryNone;
// 设置当前选中的row 的checkMark
UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType=UITableViewCellAccessoryCheckmark;
// 重置 _rowNow _secitonNow 为当前选中改行的section 和 row
_rowNow=indexPath.row;
_sectionNow=indexPath.section;
}
//===============================系统==========================
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self loadData];
[self loadTableView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)dealloc{
[_myTable release];
[_data release];
[super dealloc];
}
@end
小结: Cell 的复用是在创建时使用的,当创建新Cell 时会检测是否有可用的已经初始化的Cell,当存在时就不必再创建,而是直接使用这些“已经创建好”的而且“不再屏幕上显示”的Cell, 此时系统做的只是简单的将内容替换, Cell的定制样式会依然使用创建时定制的Style,所以要想 达到避免因为Cell复用 而产生的问题,只能在Cell创建时做出判断。即在
tableView:cellForRowAtIndexPath: 中检测并设置;