zoukankan      html  css  js  c++  java
  • ios-表视图-demo3-单选

    #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
     - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        // 不加此句时,在二级栏目点击返回时,此行会由选中状态慢慢变成非选中状态。
        // 加上此句,返回时直接就是非选中状态。
    }
    1.这里只记录一些学习笔记 2.这里只记录一些学习心得,如果心得方向有错,请留言 2.这里只记录一些日记(只为提升英语,暂时有点忙,等转行了开始写)
  • 相关阅读:
    Git学习1:Git起步
    [转载]AMOLED结构详解,BOE专家给你分析驱动补偿
    [C#] Microsoft .Net框架SerialPort类的用法与示例
    API 的历史
    AMOLED原理介紹
    [C#] SHA1校验函数用法
    示波器基本原理之三:存储深度
    示波器基本原理之二:采样率
    示波器基本原理之一:带宽
    数字转中文
  • 原文地址:https://www.cnblogs.com/liyang31tg/p/3695324.html
Copyright © 2011-2022 走看看