如何在 iPhone 的 tableview 的最后一行添加文本字段?

发布于 2024-12-20 16:56:21 字数 74 浏览 4 评论 0原文

当我按下新按钮时,我需要在表格视图的最后一行添加一个文本字段。

任何人都可以解释一下流程。

提前致谢。

when i press the new button i need to add a textfield on last row of tableview.

can any one explain the flow.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨后咖啡店 2024-12-27 16:56:21

我刚刚尝试过这种情况。

像这样创建一个布尔变量
BOOL newButtonPress = NO;

然后使用以下代码

-(IBAction)newButtonClicked:(id)sender{
if (self.newButtonPress == NO) {
//if new button press then newbuttpress is yes and reload the tableview
self.newButtonPress = YES;
[self .reloadTableview reloadData];
    }   
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(self.newButtonPress == YES)//creating noOfRows when newbutton pressed
{
    return [self.itemNames count]+1;
}
else {
    return [self.itemNames count]; 
   }
}

然后将以下代码添加到 cellForRowAtIndexPath 方法中

//when new button is pressed adding new cell
if (self.newButtonPress == YES) {
    if([self.itemNames count] == [indexPath row]){
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        //creating the textfield if new button is pressed 
        UITextField *extraField = [[UITextField alloc] initWithFrame:CGRectMake(80, 6, 100, 30)];
        extraField.borderStyle = UITextBorderStyleNone;
        extraField.textAlignment = UITextAlignmentCenter;
        [extraField becomeFirstResponder];
        [extraField setDelegate:self];
        [cell addSubview:extraField];
        [extraField release];
    }
}

Just i tried this scenario.

Create a one Boolean variable like this
BOOL newButtonPress = NO;

then fallow the below code

-(IBAction)newButtonClicked:(id)sender{
if (self.newButtonPress == NO) {
//if new button press then newbuttpress is yes and reload the tableview
self.newButtonPress = YES;
[self .reloadTableview reloadData];
    }   
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(self.newButtonPress == YES)//creating noOfRows when newbutton pressed
{
    return [self.itemNames count]+1;
}
else {
    return [self.itemNames count]; 
   }
}

Then add the below code into the cellForRowAtIndexPath method

//when new button is pressed adding new cell
if (self.newButtonPress == YES) {
    if([self.itemNames count] == [indexPath row]){
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        //creating the textfield if new button is pressed 
        UITextField *extraField = [[UITextField alloc] initWithFrame:CGRectMake(80, 6, 100, 30)];
        extraField.borderStyle = UITextBorderStyleNone;
        extraField.textAlignment = UITextAlignmentCenter;
        [extraField becomeFirstResponder];
        [extraField setDelegate:self];
        [cell addSubview:extraField];
        [extraField release];
    }
}
悲念泪 2024-12-27 16:56:21

如果您只想让文本字段出现在表格末尾,为什么不考虑使用 UITableview 的 tableFooterView 属性呢?

在这种情况下,您不必确保在委托方法中返回 UITextField。

如果您使用 InterfaceBuilder 或 XCode 4,您几乎应该能够在 Tableview 中拖放一个新的 UITextField。

If you just want the text field to appear at the end of the table, why not consider using UITableview's tableFooterView property?

In that case you don't have to ensure that you return the UITextField in your delegate methods.

You should nearly be able to drag drop a new UITextField in your Tableview if you are using InterfaceBuilder or XCode 4.

薄荷→糖丶微凉 2024-12-27 16:56:21

我对 iPhone 编码很陌生,我想分享我的想法。如果这是错误的请忽略。

cellForRowAtIndexPath 方法中,您需要设置一个 textfield
if(indexPath.row==numberOfRows-1) 条件。
在按钮按下方法中,您可以设置 textField.hidden=NO 并在 viewdidload 中设置 textField.hidden=YES

I am very new to iphone coding,i want to share my thoughts. If that is wrong please ignore.

In cellForRowAtIndexPath method u need to set a textfield in
if(indexPath.row==numberOfRows-1) condition.
In button pressed method u can set textField.hidden=NO and in viewdidload textField.hidden=YES.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文