011 在Xcode4.5上创建IOS6.0应用 (高级控件 表视图 分段表视图)

IOS中的高级控件表视图


第一步:同样是拖入控件,并对其数据源进行连线所以我就直接拿上一张的图片就不截图了。
如果有不明白的请点击连接



第二步:对H文件进行编辑实现表格控件的协议
ViewController.h
@interface ViewController : UIViewController
<UITableViewDataSource,UITableViewDelegate>{
    NSDictionary *dictionary;
    NSArray *arr;
}

@property(nonatomic,retain)NSDictionary *dictionary;
@property(nonatomic,retain)NSArray *arr;

@end


ViewController.m
@implementation ViewController

@synthesize dictionary;
@synthesize arr;


//实现表示图的方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];
    
    NSString *name = [arr objectAtIndex:section];
    NSArray *team = [dictionary objectForKey:name];
    NSString *selectedteam = [team objectAtIndex:row];
    
    NSString *message = [[NSString alloc] initWithFormat:@"你选择的号码是%@",selectedteam];
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"球队选择"
                                                    message:message delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil, nil];
    
    
    [alert show];
    [alert release];
    [message release];
    //实现点击时,让点击的那个选中慢慢消失
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

//实现索引
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return arr;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //返回段里面有几行
    NSString *name = [arr objectAtIndex:section];
    NSArray *team = [dictionary objectForKey:name];
    
    return [team count];
}
//返回数量
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [arr count];
}
//返回每个段里面的名字
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSString *name = [arr objectAtIndex:section];
    return name;
}


-(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];
    }
    
    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];
    NSString *name = [arr objectAtIndex:section];
    NSArray*team = [dictionary objectForKey:name];
    
    //返回协议的标题
    cell.textLabel.text = [team objectAtIndex:row];
    
    return cell;
    
}











- (void)viewDidLoad
{
    [super viewDidLoad];
	//下面为模式代码读取文件到代码中
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *filePath = [bundle pathForResource:@"statedictionary" ofType:@"plist"];
    NSDictionary *dic = [[NSDictionary alloc] initWithContentsOfFile:filePath];
    self.dictionary = dic;
    [dic release];
    //根据ID查询字典里面的内容并进行排序
    self.arr = [[dictionary allKeys] sortedArrayUsingSelector:@selector(compare:)];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    [dictionary release];
    [arr release];
    [super dealloc];
}

@end

最后我门就实现了分段表视图


实现分组表视图必须再分段表视图的基础上
只需要修改一个属性



大家都看到上面的视图上面都有一个索引实现起来也比较简单只要实现这段小小的代码
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return arr;
}




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shuaiyinoo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值