我自定义了一个tableview,在每个cell里面添加一个textField,当点击textField弹出键盘的时候我想把tableview向上移动,并且改变table的大小,使它不会被键盘覆盖,现在问题是当我改变table的大小的时候,tableview的数据显示出了问题,比如本来是在第二行的数据显示到了最后一行,下面是我的代码,请大家帮忙看看是什么问题。-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier] autorelease];
cell.lineBreakMode = UILineBreakModeTailTruncation;
UITextField *textfield = [[UITextField alloc] initWithFrame:CGRectMake(20, 6, 180, 32)];
textfield.font = [UIFont fontWithName:@"Arial" size:14.0f];
textfield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[textfield addTarget:self action:@selector(edit:) forControlEvents:UIControlEventEditingDidBegin]; [cell addSubview:textfield];
[textfield release];
}
return cell;
}
-(void) edit: (UIControl *)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];
[self.table setContentOffset:CGPointMake(0,200) animated:YES];
[self.table setFrame:CGRectMake(0,0,320,200)];
[UIView commitAnimations];
}
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier] autorelease];
cell.lineBreakMode = UILineBreakModeTailTruncation;
UITextField *textfield = [[UITextField alloc] initWithFrame:CGRectMake(20, 6, 180, 32)];
textfield.font = [UIFont fontWithName:@"Arial" size:14.0f];
textfield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[textfield addTarget:self action:@selector(edit:) forControlEvents:UIControlEventEditingDidBegin]; [cell addSubview:textfield];
[textfield release];
}
return cell;
}
-(void) edit: (UIControl *)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];
[self.table setContentOffset:CGPointMake(0,200) animated:YES];
[self.table setFrame:CGRectMake(0,0,320,200)];
[UIView commitAnimations];
}
解决方案 »
- ios6.0中,给导航栏加背景图片,加上后不会覆盖导航栏上的按钮
- ios开发关于sdwebimage,如何判断图片加载结束
- 如何使用普通PC(intel CPU)开发ipad2程序?
- 经过设置,启动显示不了tabbar control,请指教
- IOS 获取出口路由的MAC地址
- 怎么在ios下的网页上自动播放音乐?
- Swift语言初接触
- 黑苹果 开发
- 自定义cell上的控件关联的变量一直为nil,研究了好长时间,操作应该没有错误,求大神帮忙
- ios8 应用使用cordova3.8 的camera插件,无法拍照
- UIScrollview的setContentOffse
- 打算做一些基于iphone的应用软件,没有Mac电脑
[cell.contentView addSubview:textfield];
而不是
[cell addSubview:textfield];