点击tableView中的cell 相应的改变navgationbar的背景色

1.让tableView分组显示  并设置navgationbar的title

-(tableview *)init
{
    if(self = [super initWithStyle:UITableViewStyleGrouped])
    {
        self.title = @"ColorTransitation";
    }
    return self ;
}

2.tableView 所需的数据

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [sectionArray count];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[sectionArray objectAtIndex:section] count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ColorTranslation"];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithFrame:CGRectZero reuseIdentifier:@"ColorTranslation"];
    }
    
    NSInteger row = [indexPath row];
    NSInteger section = [indexPath section];
    
    NSArray *arrays = [[[sectionArray objectAtIndex:section] objectAtIndex:row] 
                                             componentsSeparatedByString:@"#"];
    cell.text = [arrays objectAtIndex:0];
    cell.textColor = [self getColor:[arrays objectAtIndex:1]];
    
    return cell ;
}

3。定义 得到颜色的函数

-(UIColor *)getColor:(NSString *)hexColor
{
    unsigned int red,green,blue;
    NSRange      range ;
    
    range.length = 2 ;
    range.location = 0 ;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]]scanHexInt:&red];
    range.location = 2 ;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]]scanHexInt:&green];
    range.location = 4 ;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]]scanHexInt:&blue];
    
     return [UIColor colorWithRed:(float)(red/255.0f) green:(float)(green/255.0f) 
                             blue:(float)(blue/255.0f) alpha:1.0f];
    
}

4. tableView 的委托方法

-(void)deselect
{
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = [indexPath row];
    NSInteger section = [indexPath section];
    NSArray *arrays = [[[sectionArray objectAtIndex:section]objectAtIndex:row] 
                        componentsSeparatedByString:@"#"];
    
    self.navigationController.navigationBar.tintColor = [self getColor:[arrays objectAtIndex:1]];
    [self performSelector:@selector(deselect) withObject:NULL afterDelay:0.5];
}

5.显示 tableView

-(void)createSectionList:(id)wordArray
{
    sectionArray = [[[NSMutableArray alloc]init]retain];
    for(int i=0;i<26;i++) [sectionArray addObject:[[[NSMutableArray alloc]init]retain]];
    for(NSString *word in wordArray)
    {
        if([word length]==0) continue ;
        NSRange range = [ALPHA rangeOfString:[[word substringToIndex:1]uppercaseString]];
        [[sectionArray objectAtIndex:range.location] addObject:word];
    }
}

-(void)loadView
{
    [super loadView];
    NSString *pathname = [[NSBundle mainBundle] pathForResource:@"crayons" ofType:@"txt" inDirectory:@"/"];
    NSString *wordstring = [NSString stringWithContentsOfFile:pathname];
    NSArray  *wordArray = [[wordstring componentsSeparatedByString:@"\n"]retain];
    
    [self createSectionList:wordArray];
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值