在view当中,增加toolbar,往往会导致toolbar跟着tableview一起滚动。
为了改善这种状况,通常的做法是,将toolbar和tableview都加入到父类的view当中去。
其实比较简单的参考例子如下:
NSString * strfilepath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bookname.plist"];
list = [[NSMutableArray alloc] initWithContentsOfFile:strfilepath];
CGRect rectframe = CGRectMake(0, 0, 320, 460);
mytable =[[UITableView alloc] initWithFrame:rectframe style:UITableViewStylePlain];
mytable.delegate = self;
mytable.dataSource =self;
[self.view addSubview:mytable];
CGRect tframe;
CGRect bound;
bound = [[UIScreen mainScreen] applicationFrame];
tframe = CGRectMake(0, bound.size.height - 50, bound.size.width, 50);
toolbar = [[UIToolbar alloc] initWithFrame:tframe];
toolbar.backgroundColor = [UIColor blueColor];
toolbar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
medit = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:nil];
toolbar.items = [NSArray arrayWithObjects:medit,nil];
[self.view addSubview:toolbar];
//对于tableview的具体的读取内容的做法如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell = nil;
CGRect rectFrame = CGRectMake(0, 0, 320, 30) ;
cell = [[[UITableViewCell alloc] initWithFrame:rectFrame reuseIdentifier:@""] autorelease];
NSString *strCon = [list objectAtIndex:indexPath.row];
UILabel *text = [[UILabel alloc] initWithFrame: cell.frame];
[text setFont:[UIFont boldSystemFontOfSize:20]];
[text setText:strCon];
[cell addSubview:text];
[strCon release];
return cell;
}