SetCrollIndicatorInset和setContentInset上的UITableView问题

SetCrollIndicatorInset和setContentInset上的UITableView问题,uitableview,uikeyboard,Uitableview,Uikeyboard,我有4个部分的自定义tableview 第一部分由“文本字段”组成 第二个和第三个由标签组成 第四部分由“文本区”组成 我已注册“UIKeyboardWillShowNotification”。我正在调用“keyboardWillShow”函数,如下所示 - (void)keyboardWillShow:(NSNotification *)notification { NSValue *keyboardBoundsValue = [[notification userInfo] obj

我有4个部分的自定义tableview

第一部分由“文本字段”组成

第二个和第三个由标签组成

第四部分由“文本区”组成

我已注册“UIKeyboardWillShowNotification”。我正在调用“keyboardWillShow”函数,如下所示

- (void)keyboardWillShow:(NSNotification *)notification 
{
    NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardBounds;
    [keyboardBoundsValue getValue:&keyboardBounds];
    UIEdgeInsets e = UIEdgeInsetsMake(0, 0, keyboardBounds.size.height + 80, 0);
    [eventTableView setScrollIndicatorInsets:e];
    [eventTableView setContentInset:e];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if([indexPath section] == 0 || [indexPath section] == 4)
    {
        if (cell == nil) 
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

            if([indexPath section] == 0)
            {
                UITextField* titleTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 10, 280, cell.bounds.size.height)];
                [titleTextField setTag:201];
                [titleTextField setPlaceholder:@"Title"];
                [titleTextField setEnablesReturnKeyAutomatically:YES];
                [titleTextField setDelegate:self];
                [titleTextField setClipsToBounds:YES];
                [[cell contentView]addSubview:titleTextField];

                [titleTextField release];
            }
            else if([indexPath section] == 4)
            {
                UITextView* messageTextView = [[UITextView alloc]initWithFrame:CGRectMake(10, 5, 280, 2 * cell.bounds.size.height)];
                [messageTextView setTag:206];
                [messageTextView setEnablesReturnKeyAutomatically:YES];
                [messageTextView setBackgroundColor:[UIColor clearColor]];
                [messageTextView setDelegate:self];
                [messageTextView setClipsToBounds:YES];
                [[cell contentView]addSubview:messageTextView];

                [messageTextView release];
            }
        }
    }
    else
    {
        if (cell == nil) 
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
            [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

            if([indexPath section] == 1)
            {
                if([indexPath row] == 0)
                {
                    cell.textLabel.text = @"Start";
                    [cell setTag:202];
                }
                else if([indexPath row] == 1)
                {
                    cell.textLabel.text = @"End";
                    [cell setTag:203];
                }
            }

            else if([indexPath section] == 2)
            {       
                cell.textLabel.text = @"Reminder";
                [cell setTag:204];
            }
            else if([indexPath section] == 3)
            {        
                cell.textLabel.text = @"Add Photo";
                [cell setTag:205];
            }
        }
    }

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    return cell;
}
将自定义单元格添加到tableview,如下所示

- (void)keyboardWillShow:(NSNotification *)notification 
{
    NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardBounds;
    [keyboardBoundsValue getValue:&keyboardBounds];
    UIEdgeInsets e = UIEdgeInsetsMake(0, 0, keyboardBounds.size.height + 80, 0);
    [eventTableView setScrollIndicatorInsets:e];
    [eventTableView setContentInset:e];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if([indexPath section] == 0 || [indexPath section] == 4)
    {
        if (cell == nil) 
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

            if([indexPath section] == 0)
            {
                UITextField* titleTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 10, 280, cell.bounds.size.height)];
                [titleTextField setTag:201];
                [titleTextField setPlaceholder:@"Title"];
                [titleTextField setEnablesReturnKeyAutomatically:YES];
                [titleTextField setDelegate:self];
                [titleTextField setClipsToBounds:YES];
                [[cell contentView]addSubview:titleTextField];

                [titleTextField release];
            }
            else if([indexPath section] == 4)
            {
                UITextView* messageTextView = [[UITextView alloc]initWithFrame:CGRectMake(10, 5, 280, 2 * cell.bounds.size.height)];
                [messageTextView setTag:206];
                [messageTextView setEnablesReturnKeyAutomatically:YES];
                [messageTextView setBackgroundColor:[UIColor clearColor]];
                [messageTextView setDelegate:self];
                [messageTextView setClipsToBounds:YES];
                [[cell contentView]addSubview:messageTextView];

                [messageTextView release];
            }
        }
    }
    else
    {
        if (cell == nil) 
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
            [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

            if([indexPath section] == 1)
            {
                if([indexPath row] == 0)
                {
                    cell.textLabel.text = @"Start";
                    [cell setTag:202];
                }
                else if([indexPath row] == 1)
                {
                    cell.textLabel.text = @"End";
                    [cell setTag:203];
                }
            }

            else if([indexPath section] == 2)
            {       
                cell.textLabel.text = @"Reminder";
                [cell setTag:204];
            }
            else if([indexPath section] == 3)
            {        
                cell.textLabel.text = @"Add Photo";
                [cell setTag:205];
            }
        }
    }

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    return cell;
}
当我单击textView时,会收到键盘通知,因此tableview会向上滚动以适应键盘。但当我在“textviewDidEndEditing”上退出键盘时,tableview的单元格交换

这是TextViewDiEndediting的代码

- (void)textViewDidEndEditing:(UITextView *)textView
{
    [UIView beginAnimations:NULL context:nil];
    [UIView setAnimationDuration:0.5];
    UIEdgeInsets e = UIEdgeInsetsMake(0, 0, 0, 0);
    [eventTableView setScrollIndicatorInsets:e];
    [eventTableView setContentInset:e];
    [UIView commitAnimations];
}

请帮我解决这个问题。我不知道为什么当键盘出现,当我辞去textfield和textView的第一响应者时,这些单元格会被交换。提前谢谢。

我自己找到了答案。当我使用相同的“CellIdentifier”分配TextField和TextArea时,它的创建问题

因此,我在xib中创建了2个自定义UITableViewCell。一个用于自定义TextField单元格,一个用于自定义TextView单元格,并分别给出了单元格标识符“TitleCell”和“MessageCell”。现在我转到CellForRowatineXpath函数,并将“TitleCell”作为[indexPath节]==0的标识符,将“MessageCell”作为[indexPath节]==4的标识符

就这样。我的问题解决了。谢谢:)

试试这个

-(void) textFieldDidBeginEditing:(UITextField *)textField
{
    UITableViewCell *cell = (UITableViewCell *)[textField superview];
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
    return YES;
}