【问题标题】:Recreate a uitableview section dynamically on a uibutton`s click在 uibutton 的单击上动态重新创建 uitableview 部分
【发布时间】:2015-07-14 06:14:24
【问题描述】:

我需要在 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;
 }

【问题讨论】:

  • 请帮帮我,当用户单击加号按钮或单独的新表格视图时,您想创建一个新行吗?如果是,您打算在哪里添加它,因为您已经在屏幕上有一个表格视图。
  • 按钮的touchUpInside事件调用的方法是什么?
  • @ Parvez Belim - 我需要重新创建 uitableview 的所有行,可能在每个类型 uiButton 被点击的新部分中
  • @ ZeMoon- On UIButtons Action 我想创建一个新部分,它重复上一部分的所有行项目。
  • 我建议为现有的 tableview 创建新的 Section 而不是创建新的 table view。增加屋顶部分,并在模型中进行更改并重新加载 tableview

标签: ios objective-c iphone uitableview uibutton


【解决方案1】:

在.h中为tableview创建一个整数变量index和属性

@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];
}

在委托方法中使用标签

编辑:正如您要求在表格视图中添加新部分而不是新表格视图 您必须首先创建分组样式的表格视图

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

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

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

然后,您必须为分组表视图实现此委托方法

- (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
}

【讨论】:

  • Lalit Kumar - 不想每次都创建表格,我想创建一个部分
  • 你在创建组tableview吗?
  • 每次点击添加按钮都需要重新加载tableview
  • 不,它很简单的 UItableVIew @ Lalil Kumar
  • 所以,简单的 UITableView 只有一个部分,你需要创建你的 tableview 分组
【解决方案2】:
//
//  ViewController.m
//  DynamicNewSections
//
//  Created by J Pulikkottil on 14/07/15.
//  Copyright (c) 2015 J Pulikkottil. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property(nonatomic) NSMutableArray *arrayData;
@property (strong, nonatomic) IBOutlet UITableView *tableViewTest;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

self.arrayData = [NSMutableArray array];


//section1
[self addSectionArray];
}
- (IBAction)addSection:(UIButton *)sender {

[self addSectionArray];
[self.tableViewTest reloadData];
}

- (void) addSectionArray{

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"Height",@"label", @"", @"value", nil];
NSArray *arraysection = [NSArray arrayWithObjects:dict, nil];

[self.arrayData addObject:arraysection];

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{


UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableViewTest.frame.size.width, 45)];
view.backgroundColor = [UIColor yellowColor];
return view;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

// Return the number of sections.
return [self.arrayData count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

// Return the number of rows in the section.
return [[self.arrayData objectAtIndex:section] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellDetails" forIndexPath:indexPath];

NSArray *arraysection = [self.arrayData objectAtIndex:indexPath.section];
NSDictionary *dict = [arraysection objectAtIndex:indexPath.row];

cell.textLabel.text = [dict valueForKey:@"label"];
cell.detailTextLabel.text = [dict valueForKey:@"value"];
return cell;
}


@end

【讨论】:

  • 就像下面的答案,在 addSection 中,您可以使用 insertSections 方法并仅重新加载新部分而不是重新加载整个表格。
【解决方案3】:

试试这个:

  • 只有一个属性(例如numberOfSections
  • viewDidLoad 中将其设置为1
  • numberOfSectionsInTableView:return self.numberOfSections;
  • UIButton 的 IBAction 中添加以下代码:


self.numberOfSections++;
[self.tableView beginUpdates];
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:self.numberOfSections-1]
              withRowAnimation:UITableViewRowAnimationBottom];
[self.tableView endUpdates];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多