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;
}