我需要在单击 UIButton 时重新创建 UITableView 部分。这是一个例子。
并且在按钮点击中它应该像这样创建。
但是我无法实现这个。
请帮助。 我只是创建一个带有 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/