【问题标题】:How to add row in a table on a click event of a Button如何在按钮的单击事件上在表格中添加行
【发布时间】:2011-07-26 16:58:00
【问题描述】:

我想在 Button 的单击事件的表格中添加一行。为此,我使用以下代码。但它不起作用。

-(void)translation:(id)sender
{
    [self tableView:mainTableView numberOfRowsInSection:1];
    i+1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return i;
} 

其中 i 是一个全局变量,它在 viewdidload 函数中初始化为 1; 提前感谢任何类型的建议。谢谢

【问题讨论】:

    标签: iphone ipad uitableview


    【解决方案1】:

    您可以像使用 i 一样在按钮事件递增实例或全局变量时执行此操作,因此代码将如下所示

    -(void)translation:(id)sender
    {
       i = i+1;
    //This function will automatically call numberofrows and cellforrowatindexpath methods of tableView
       [tableView reloadData];
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        // Return the number of rows in the section.
        return i;
    } 
    

    【讨论】:

    • 感谢您的帮助。例如,如果我有两个表并且我想同时在两个表上添加行而不是我该怎么做。我正在做的事情是在创建表时为每个表分配一个不同的标签,但只为 2 个表创建行。
    • 好吧,代码对两者都适用,只需在两个 tableView 实例上调用 reloadData,就像你有 tableView1 和 tableView2 然后在按钮上点击你调用 [tableView1 reloadData]; [tableView2 重载数据];但请确保两个 tableView 的数据源应该相同。还请先阅读 Vince 建议的一些 UITableView 编程指南
    • 如果我根据标签属性区分表格。那么如何在两个表中添加行。
    • 添加行不需要区分
    【解决方案2】:

    一个小建议:Table View Programming Guide for iOS,请先阅读。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-15
      • 2013-11-14
      • 2013-12-28
      • 2018-06-16
      • 2023-03-15
      • 1970-01-01
      • 2015-05-11
      相关资源
      最近更新 更多