ios - 单击 uibutton 动态重新创建 uitableview 部分

标签 ios objective-c iphone uitableview uibutton

我需要在单击 UIButton 时重新创建 UITableView 部分。这是一个例子。

enter image description here

并且在按钮点击中它应该像这样创建。

enter image description here

但是我无法实现这个。

请帮助。 我只是创建一个带有 textLabel 的简单 UITableView。

cellForRowAtIndexPath 的代码是

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
     {
     static NSString *simpleTableIdentifier = @"SimpleTableItem";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}

cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
 }

最佳答案

在.h中为tableview创建一个整型变量index和property

@property UITableView *tableView;

然后在-(void)viewDidLoad设置index=0;

现在,编写一个在您点击添加按钮时调用的方法

-(void)newTableView
{
tableView = [UITableView alloc] initWithFrame:CGRectMake(x,y+height*index,width,height)]; 
//other code related to table
tableView.tag = ++index;
[self.view addSubview:tableView];
}

Use tag in delegate methods

编辑:根据您的要求在表格 View 中添加新部分而不是新表格 View 您必须先创建分组样式的 TableView

tableView = [UITableView alloc] initWithFrame:(CGRect)frame style:UITableViewStyleGrouped];

现在,当你点击那个添加按钮时调用一个方法

-(void)newSection
{
  count++;
  [tableView reloadData];
}

然后,您必须为分组 TableView 实现此委托(delegate)方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"title";
}

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
   //same as your previous code
    }


- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //same as your previous code
}

关于ios - 单击 uibutton 动态重新创建 uitableview 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31398967/

相关文章:

ios - 为什么在测试 NSNull 之前转换为对象?

iphone - Facebook iOS 似乎在单击“确定”后返回应用程序时丢失了 session 您已经授权了应用程序

iphone - 为什么这个 View Controller 不能设置下一个 View Controller 的文本字段?

ios - 如何使用 Phonegap 处理 iOS 上的后退按钮?

ios - iOS 应用程序可以从任何蓝牙设备接收数据吗?

javascript - iOS UIwebview 中的自动化浏览器

ios - 遍历已解析的数组

iphone - 使固定大小的圆形按钮作为iPhone SDK中的图像

ios - Objective-C NSData 返回 nil

使用 ios 5 的 iphone 5 NIB