大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > IOS技巧 > IOS开发之表视图添加索引

IOS开发之表视图添加索引

关键词:视图索引添加开发IOS  阅读(829) 赞(14)

[摘要]本文是对IOS开发之表视图添加索引的讲解,对学习IOS苹果软件开发有所帮助,与大家分享。

我们要实现的效果如下。

//分为多少个分组
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
return[[dictallKeys]count];
}
//每个分组的数据单元个数
-(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section
{
switch(section)
{
case0:
{
return[[dictobjectForKey:@"Group1"]count];
}
case1:
{
return[[dictobjectForKey:@"Group2"]count];
}
case2:
{
return[[dictobjectForKey:@"Group3"]count];
}
}
return0;
}
//分组的标题,不实现下面的方法,不显示分组标题
-(NSString*)tableView:(UITableView*)tableViewtitleForHeaderInSection:(NSInteger)section
{
//dictallKeys取出的keyarr无顺序,需进行排序
NSArray*arr=[[dictallKeys]sortedArrayUsingSelector:@selector(compare:)];
return[arrobjectAtIndex:section];
}
//列表右侧的索引提示,不实现下面的方法,不显示右侧索引
-(NSArray*)sectionIndexTitlesForTableView:(UITableView*)tableView
{
//dictallKeys取出的keyarr无顺序,需进行排序
NSArray*arr=[[dictallKeys]sortedArrayUsingSelector:@selector(compare:)];
returnarr;
}

-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath
{
staticNSString*CellIdentifier=@"myTableCell";
UITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:CellIdentifier];

NSUIntegerrow=[indexPathrow];
NSUIntegersection=[indexPathsection];
NSArray*arr;

switch(section){
case0:
arr=[dictobjectForKey:@"Group1"];
break;
case1:
arr=[dictobjectForKey:@"Group2"];
break;
case2:
arr=[dictobjectForKey:@"Group3"];
break;
default:
break;
}

NSDictionary*rowDict=[arrobjectAtIndex:row];
cell.textLabel.text=[rowDictobjectForKey:@"itemName"];
NSLog(@"cell.label.text=%@",[rowDictobjectForKey:@"itemName"]);

NSString*imagePath=[rowDictobjectForKey:@"itemImagePath"];
cell.imageView.image=[UIImageimageNamed:imagePath];
NSLog(@"cell.image.image=%@",imagePath);

cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

returncell;
}

//选中Cell响应事件
-(void)tableView:(UITableView*)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath{
[tableViewdeselectRowAtIndexPath:indexPathanimated:YES];//选中后的反显颜色即刻消失
NSUIntegerrow=[indexPathrow];
NSUIntegersection=[indexPathsection];
NSArray*arr;

switch(section){
case0:
arr=[dictobjectForKey:@"Group1"];
break;
case1:
arr=[dictobjectForKey:@"Group2"];
break;
case2:
arr=[dictobjectForKey:@"Group3"];
break;
default:
break;
}

NSDictionary*rowDict=[arrobjectAtIndex:row];
NSString*userName=[rowDictobjectForKey:@"itemName"];
NSLog(@"userName=%@",userName);
}
//分为多少个分组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return [[dict allKeys] count];
}
//每个分组的数据单元个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch(section)
    {
        case 0:
        {
            return [[dict objectForKey:@"Group1"] count];
        }
        case 1:
        {
            return [[dict objectForKey:@"Group2"] count];
        }
        case 2:
        {
            return [[dict objectForKey:@"Group3"] count];
        }
    }
    return 0;
}
//分组的标题,不实现下面的方法,不显示分组标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    //dict allKeys取出的key arr无顺序,需进行排序
    NSArray *arr = [[dict allKeys] sortedArrayUsingSelector:@selector(compare:)];
	return [arr objectAtIndex:section];
}
//列表右侧的索引提示,不实现下面的方法,不显示右侧索引
-(NSArray *) sectionIndexTitlesForTableView: (UITableView *) tableView
{
    //dict allKeys取出的key arr无顺序,需进行排序
    NSArray *arr = [[dict allKeys] sortedArrayUsingSelector:@selector(compare:)];
    return arr;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"myTableCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    NSUInteger row = [indexPath row];
    NSUInteger section = [indexPath section];
    NSArray *arr;
    
    switch (section) {
        case 0:
            arr = [dict objectForKey:@"Group1"];
            break;
        case 1:
            arr = [dict objectForKey:@"Group2"];
            break;
        case 2:
            arr = [dict objectForKey:@"Group3"];
            break;
        default:
            break;
    }
    
    NSDictionary *rowDict = [arr objectAtIndex:row];
    cell.textLabel.text =  [rowDict objectForKey:@"itemName"];
    NSLog(@"cell.label.text =  %@",[rowDict objectForKey:@"itemName"]);
    
    NSString *imagePath = [rowDict objectForKey:@"itemImagePath"];
    cell.imageView.image = [UIImage imageNamed:imagePath];
    NSLog(@"cell.image.image  =  %@",imagePath);
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

//选中Cell响应事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失
    NSUInteger row = [indexPath row];
    NSUInteger section = [indexPath section];
    NSArray *arr;
    
    switch (section) {
        case 0:
            arr = [dict objectForKey:@"Group1"];
            break;
        case 1:
            arr = [dict objectForKey:@"Group2"];
            break;
        case 2:
            arr = [dict objectForKey:@"Group3"];
            break;
        default:
            break;
    }
    
    NSDictionary *rowDict = [arr objectAtIndex:row];
    NSString *userName =  [rowDict objectForKey:@"itemName"];
    NSLog(@"userName=%@",userName);
}
    UITableView.zip (22.2 KB)
踩1 踩一下 顶14 赞一个

相关IOS技巧推荐



相关评论