ios-表视图-demo3-单选
时间:2014-04-28 17:19:46
收藏:0
阅读:596
#import "RootViewController.h" @interface RootViewController () @end @implementation RootViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)loadView { UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame]; self.view = view; [view release]; _listArray = [[UIFont familyNames] retain]; _tableView = [[UITableView alloc] initWithFrame:view.bounds style:UITableViewStylePlain]; _tableView.dataSource = self; // 设置数据源 _tableView.delegate = self; // 设置委托 [self.view addSubview:_tableView]; _index = -1; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - TableView Datasource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; { return [_listArray count]; } // section 中包含row的数量 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 定义一个静态标识符 static NSString *cellIdentifier = @"cell"; // 检查是否限制单元格 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; // 创建单元格 if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease]; } // 给cell内容赋值 NSString *fontName = _listArray[indexPath.row]; cell.textLabel.text = fontName; cell.textLabel.font = [UIFont fontWithName:fontName size:18]; if (_index == indexPath.row) { cell.accessoryType = UITableViewCellAccessoryCheckmark; }else { cell.accessoryType = UITableViewCellAccessoryNone; } return cell; } // 创建单元格 /* - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { } // 表视图当中存在secion的个数,默认是1个 */ /* question * 1、单选 * 2、重用 */ #pragma mark - UITableViewDelegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // NSIndexPath -> max row 取消上一次选中 NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:_index inSection:0]; UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:lastIndex]; lastCell.accessoryType = UITableViewCellAccessoryNone; // 用户选中了新的一行 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.accessoryType = UITableViewCellAccessoryCheckmark; _index = indexPath.row; [_tableView performSelector:@selector(deselectRowAtIndexPath:animated:) withObject:indexPath afterDelay:.5]; } - (void)dealloc { [_tableView release]; _tableView = nil; [super dealloc]; } @end
评论(0)