UI效果IOS开发路上的故事ios专题

tableview26字母右边栏-swift版与OC版

2016-07-21  本文已影响530人  HeavenWong

OC版

//改变索引的颜色
    self.tableView.sectionIndexColor = [UIColor redColor];
    //改变索引选中的背景颜色
   self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];

//索引数组
    _dataSource = [[NSMutableArray alloc] init] ;
    //tableview 数据源
    _dataBase = [[NSMutableArray alloc] init] ;
    
    //初始化数据
    for(char c = 'A'; c <= 'Z'; c++ )
    {
        [_dataSource addObject:[NSString stringWithFormat:@"%c",c]];
        [_dataBase addObject:[NSString stringWithFormat:@"%c",c]];
        [_dataBase addObject:[NSString stringWithFormat:@"%c",c]];
        [_dataBase addObject:[NSString stringWithFormat:@"%c",c]];
    }

//返回索引数组
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    
    return _dataSource;
}

//响应点击索引时的委托方法    `此方法不写无影响`
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    NSInteger count = 0;
    
    NSLog(@"%@-%ld",title,(long)index);
    
    for(NSString *character in _dataSource)
    {
        if([character isEqualToString:title])
        {
            return count;
        }
        count ++;
    }
    return 0;
}

效果图


swift版

//索引数组
    private lazy var dataSource : NSArray = {
        let list:[String] = [
            "A", "B", "C", "D", "E", "F", "G",
            "H", "I", "J", "K", "L", "M", "N",
            "O", "P", "Q", "R", "S", "T",
            "U", "V", "W", "X", "Y", "Z"]
        
        return list
    }()

// 遵守UITableViewDelegate, UITableViewDataSource协议

    //实现索引数据源代理方法
    override func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
        return dataSource as? [String]
    }
    
    //点击索引,移动TableView的组位置
    override func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String,
                   atIndex index: Int) -> Int {
        var tpIndex:Int = 0
        //遍历索引值
        for character in dataSource{
            //判断索引值和组名称相等,返回组坐标
            if character as! String == title{
                return tpIndex
            }
            tpIndex += 1
        }
        return 0
    }
    
    // UITableViewDataSource协议中的方法,该方法的返回值决定指定分区的头部
    override func tableView(tableView:UITableView, titleForHeaderInSection
        section:Int)->String?
    {
        return dataSource[section] as? String;
    }

// MARK: - Table view data source
    // 行高
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 46
    }
// 组头高
    override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 20
    }
    // 组数
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        
        return 3
    }
    // cell个数
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    // cell的内容
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier("default")
        
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCellWithIdentifier(changeBranchCellID) as! ChooseBranchCell
            return cell
        }else {
            
            if indexPath.section == 1 {
                cell?.textLabel?.text = "杭州"
                return cell!
            }else {
                cell?.textLabel?.text = "无锡"
                return cell!
            }
        }
        
    }
    
效果图2
上一篇 下一篇

猜你喜欢

热点阅读