微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

iPhone,UITableView中的UITextField

如何解决iPhone,UITableView中的UITextField

| 我正在尝试在表视图中添加文本字段。除了最后一行,我希望每行都有一个标签一个文本字段。我要在最后一行切换。 问题在于文本字段在其余行上重叠。我将我的文本字段代码移到了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,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;

}
当我上下滚动时,文本字段是重叠的,这意味着在第一行输入文本并向下滚动之后,我也可以看到该文本字段也在最后一行复制。     

解决方法

        单元格重用导致文本字段重叠。每次重复使用单元格时,您都将添加一个文本字段或一个开关。这些正在堆积。您需要先删除较旧的子视图,该子视图要么是开关,要么是文本字段,然后再添加一个。     ,        为什么要使用两个tableview只是这样做
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {                          
if(indexPath.row==[tableviewarray count]){
//dont add label or textfield
}
else{
// add label or textfield
}

}
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。