Xcode iphone,UITableView中的UITextField
我正在尝试在tableview中添加文本字段。除了最后一行之外,我希望每一行都有一个标签和一个文本字段。我想在最后一排换乘。 问题是文本字段在其余行上重叠。我将我的文本字段代码移到了if(cell==nil)中,但这不起作用。。。这是我的密码Xcode iphone,UITableView中的UITextField,xcode,uitableview,uitextfield,Xcode,Uitableview,Uitextfield,我正在尝试在tableview中添加文本字段。除了最后一行之外,我希望每一行都有一个标签和一个文本字段。我想在最后一排换乘。 问题是文本字段在其余行上重叠。我将我的文本字段代码移到了if(cell==nil)中,但这不起作用。。。这是我的密码 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *MyIdentifier
- (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;
}
当我上下滚动时,文本字段是重叠的,我的意思是在第一行输入文本并向下滚动后,我可以看到文本字段也在最后一行复制。单元格重用导致文本字段重叠。每次重复使用单元格时,都会添加一个文本字段或开关。这些正在堆积。在添加之前,您需要删除旧的子视图,它可能是开关或文本字段 为什么要使用两个表视图只需这样做
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row==[tableviewarray count]){
//dont add label or textfield
}
else{
// add label or textfield
}
}
创建文本字段/开关时,为其指定一个标记。说45。然后使用
UIView*theView=[cell view with tag:45]检索它代码>。既然现在您拥有了视图,只需执行[视图从SuperView移除]代码>。或者,如果它是一个文本字段,并且需要插入一个文本字段,则可以将其设置重置为所需设置。这样,您就不需要再次创建它了。