IOS开发---设置单选的Cell

#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,取消上一个rowCheckMark  2,设置当前的rowcheckmark  3,重置_rowNow _sectionNow

     *  可以达到 只让当前选中的rowCheckMark 标识,其它的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: 中检测并设置;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值