IOS UITableView实现单选功能

IOS UITableView实现单选功能

   
   
// 实现功能:没有任何操作前默认选中第0行。操作后记录选择最后的行,下次进入会显示上一次选中的行

    
    
- ( void ) viewDidAppear: ( BOOL ) animated {
[ super viewDidAppear : animated ];
// 读取上一次选择的cell
NSUserDefaults * defaults = [ NSUserDefaults standardUserDefaults ];
NSIndexPath * indexpath = [ NSIndexPath indexPathForRow : [ defaults integerForKey : @"selectedRow" ] inSection : 0 ];
// 设置默认选中行
[ self . tableView selectRowAtIndexPath : indexpath animated : YES scrollPosition : UITableViewScrollPositionNone ];
[ self tableView : self . tableView didSelectRowAtIndexPath : indexpath ];
[ self . tableView reloadData ];
}

- ( UITableViewCell * ) tableView: ( UITableView * ) tableView cellForRowAtIndexPath: ( NSIndexPath * ) indexPath
{
static NSString * reuseID = @"WMLauageCell" ;
WMLauageCell * cell = [ tableView dequeueReusableCellWithIdentifier : reuseID ];
if ( cell == nil ){
cell = [[ WMLauageCell alloc ] initWithStyle : UITableViewCellStyleDefault reuseIdentifier : reuseID ];
cell . contentView . backgroundColor = [ UIColor lightTextColor ];
UIView * backGroundView = [[ UIView alloc ] init ];
backGroundView . backgroundColor = [ UIColor lightTextColor ];
cell . selectedBackgroundView = backGroundView ;
}
cell . language . text = self . languageArray [ indexPath . row ];
cell . indationIcon . image = SET_IMAGE ( @"check_box_s" );
NSUserDefaults * defaults = [ NSUserDefaults standardUserDefaults ];
NSIndexPath * path = [ NSIndexPath indexPathForRow : [ defaults integerForKey : @"selectedRow" ] inSection : 0 ];
if ( self . isFirst && path . row == indexPath . row ){
self . isFirst = NO ;
cell . indationIcon . hidden = NO ;
}
return cell ;
}


- ( void ) tableView: ( UITableView * ) tableView didSelectRowAtIndexPath: ( NSIndexPath * ) indexPath {
[[ NSUserDefaults standardUserDefaults ] setInteger : indexPath . row forKey : @"selectedRow" ];
[[ NSUserDefaults standardUserDefaults ] synchronize ];
NSUInteger newRow = indexPath . row ;
NSUInteger oldRow = [ self . lastIndexPath row ];
if ( newRow != oldRow )
{
WMLauageCell * newCell = [ tableView cellForRowAtIndexPath :
indexPath ];
newCell . indationIcon . hidden = NO ;
WMLauageCell * oldCell = [ tableView cellForRowAtIndexPath :
self . lastIndexPath ];
oldCell . indationIcon . hidden = YES ;
self . lastIndexPath = indexPath ;
}

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值