iphone, UITextField в UITableView - PullRequest
       1

iphone, UITextField в UITableView

0 голосов
/ 30 мая 2011

Я пытаюсь добавить текстовые поля в виде таблицы. Я хочу метку и текстовое поле в каждой строке, кроме последней. Я хочу переключатель в последнем ряду. Проблема в том, что текстовые поля перекрываются на остальных строках. Я переместил свой код текстового поля в if (cell == nil) , но это не сработало ... вот мой код

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *MyIdentifier = @"mainMenuIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
    [cell setSelectedBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"highlightstrip.png"]]];


    if (tableView.tag == 1) {

        UILabel *lblName = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 90, 20)];
        lblName.textAlignment = UITextAlignmentLeft;
        lblName.font = [UIFont boldSystemFontOfSize:14];
        lblName.backgroundColor = [UIColor clearColor];
        lblName.tag = 31;
        [cell.contentView addSubview:lblName];
        [lblName release];

    }

}

    if (tableView.tag == 1) {

        [(UILabel *) [cell viewWithTag:31] setText:[tableElements objectAtIndex:indexPath.row]];
// check if the last row
        if (indexPath.row == 10) {
            newsSwtich = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
            [newsSwtich addTarget:self action:@selector(switchToggled:) forControlEvents: UIControlEventTouchUpInside];
            cell.accessoryView = newsSwtich;
        }
        else {

                UITextField *tempTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 10, 200, 20)];
                tempTextField.delegate = self;
                //  tempTextField.placeholder = [tableElements objectAtIndex:indexPath.row];
                tempTextField.font = [UIFont fontWithName:@"Arial" size:14];
                tempTextField.textAlignment = UITextAlignmentLeft;
                tempTextField.tag = indexPath.row;
                tempTextField.autocorrectionType = UITextAutocorrectionTypeNo;  // no auto correction support
                tempTextField.keyboardType = UIKeyboardTypeDefault;  // type of the keyboard
                tempTextField.returnKeyType = UIReturnKeyDone;  // type of the return key
                tempTextField.clearButtonMode = UITextFieldViewModeWhileEditing;    // has a clear 'x' button to the right
                [cell.contentView addSubview:tempTextField];
                [tempTextField release];


            cell.accessoryView = UITableViewCellAccessoryNone;
        }


cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;

}

Когда я прокручиваю вверх и вниз текстовые поля перекрываются, я имею в виду, что после ввода текста в первой строке и прокрутки вниз, я вижу, что текстовое поле также копируется в последнюю строку.

Ответы [ 2 ]

1 голос
/ 31 мая 2011

Повторное использование ячеек приводит к перекрытию текстовых полей. Каждый раз, когда ячейка используется повторно, вы добавляете текстовое поле или переключатель. Это накапливается. Вам нужно будет удалить старое подпредставление, которое будет либо переключателем, либо текстовым полем, прежде чем добавить его.

0 голосов
/ 31 января 2013

почему вы используете две таблицы, просто сделайте это

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {                          
if(indexPath.row==[tableviewarray count]){
//dont add label or textfield
}
else{
// add label or textfield
}

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...