IOS - 如何在分组表视图中混合使用文本字段和文本视图

时间:2012-11-07 20:49:42

标签: uitableview ios6

首先,这可能吗?

我在iPad详细信息视图中有代码cellAtIndexPath方法,如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //NSLog(@"%s", __FUNCTION__);
    DetailCell *cell = nil;
    NSInteger tag = 0;
    NSString *text = nil;
    NSString *placeholder = nil;


    NSUInteger section = [indexPath section];
    switch (section)
    {
        case TitleSection:
        {
            cell = [self titleTextField];
            text = ...
            tag = ...
            placeholder = @"Title";
            break;
        }
        case UserSection:
        {
            if ([indexPath row] == 0) {

                    cell = [self usernameTextField];
                    text = ....
                    tag = ....
                    placeholder = ...
                    break;
            } else {

                    cell = [self passwordTextField];
                    text = ....
                    tag = .....
                    placeholder = ...
                    break;
            }


        }
        case CompanySection:
        {
            switch ([indexPath row]) {
                case 0:
                    cell = [self companyTextField];
                    text = .......
                    tag = ....
                    placeholder = ......
                    break;
                case 1:

                    break;
                case 2:

                    break;

            }
            break;
        }

        case NotesSection:
        {
            cell = [self notesTextView];
            text = ...
            tag = ...
            placeholder = @"Tap to Enter some Notes";
            break;
        }
    }



    UITextField *textField = [cell textField];
    [textField setTag:tag];
    [textField setText:text];
    [textField setPlaceholder:placeholder];

    return cell;

我希望Notes部分是UITextView类,而其他所有部分都是UITextField类。

之前必须这样做,但我在这里找不到参考。请指出我正确的方向..

1 个答案:

答案 0 :(得分:0)

我通过继承细胞来解决这个问题:

CGRect rect = CGRectInset(bounds, 20.0, 10.0);
    textField = [[UITextField alloc] initWithFrame:rect];


    [textField setReturnKeyType:UIReturnKeyNext];

    //  Make the clear button appear automatically.
    [textField setClearButtonMode:UITextFieldViewModeWhileEditing];
    [textField setBackgroundColor:[UIColor clearColor]];
    [textField setFont:[UIFont fontWithName:@"Arial" size:18]];
    [textField setOpaque:YES];



    textView = [[UITextView alloc] initWithFrame:bounds];

    //  Set the keyboard's return key label to 'Next'.
    //
    [textView setReturnKeyType:UIReturnKeyNext];


    [textView setScrollEnabled:YES];
    [textView setBackgroundColor:[UIColor clearColor]];
    [textView setFont:[UIFont fontWithName:@"Arial" size:18]];
    [textView setOpaque:YES];


    [[self contentView] addSubview:textField];
    [self setTextField:textField];

    [[self contentView] addSubview:textView];
    [self setTextView:textView];



and then looking for specific sections of my table in cellForRowAtIndexPath:


case CompanySection:
        {
            switch ([indexPath row]) {
                case 0:
                    cell = [self companyTextField];
                    text = [entity company];
                    tag = EntityCompany;
                    placeholder = @"Company";
                    break;
                case 1:...

                case 2:...

                case 3:...
                    cell = [self emailTextField];
                    text = [entity emailAddress];
                    tag = EntityEmail;
                    placeholder = @"Email";
                    break;
            }
            break;
        }

        case NotesSection:
        {

            cell = [self notesTextView];
            text = [entity notes];
            tag = EntityNotes;
            //placeholder = @"Tap to Enter some Notes";
            break;
        }

希望这有助于某人..