【问题标题】:Dynamically add tableview cell one by one by button click in a single tableview在单个tableview中通过按钮单击动态添加tableview单元格
【发布时间】:2015-06-09 18:34:48
【问题描述】:

我。我尝试了很多但无法成功执行。 ii.在 tableview 单元格中,需要显示 3 3 个字段。一个图像视图,button1 -->拍照按钮,button2--->浏览按钮。 三。第一次 tableview 应该显示一个带有一行的自定义单元格。 iv.当用户单击位于 tableview 之外的“添加新按钮”时,将创建一个包含所有上述 3 个字段(图像视图、按钮 1、按钮 2)的新行 v.“添加新按钮”的点击次数,将创建包含所有上述3个字段的新行。 六。我可以使用包含以上 3 个字段但无法成功处理自定义单元格的简单图像视图动态成功创建上述所有内容。

七。我需要再次设置每个单元格的标签,broswe 按钮,拍照按钮,以便在点击时获取标签值。

【问题讨论】:

  • 你需要比这更清楚。请参考此处发布的其他问题并重构您的问题。

标签: ios xcode uitableview dynamically-generated tableviewcell


【解决方案1】:

表格视图通过添加委托和数据源来工作。假设您的表视图有一个所有者作为视图控制器,并且委托和数据源都是视图控制器本身。您需要做的就是实现这些数据源方法以返回适当的数据,然后您应该在表格视图上调用reloadData,或者如果您想要一些额外的工作来看起来更好,请检查如何在网络上添加动画行。

这是一个非常简单且未优化的示例,但非常简短且易于阅读。我希望它可以帮助您走上正轨:

@interface MyViewController()<UITableViewDataSource, UITableViewDelegate>

@property UITableView *tableView;
@property NSArray *myCells;

@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self; // could be done in storyboard
    self.tableView.dataSource = self; // could be done in storyboard
    [self addACell];
}
- (void)addCellButtonPressed:(id)sender {
    [self addACell];
}
- (void)addACell {
    MyCell *cell = [[MyCell alloc] init];
    [cell.button1 addTarget:self action:@selector(cellButton1Pressed:) forControlEvents:UIControlEventTouchUpInside];
    [cell.button2 addTarget:self action:@selector(cellButton2Pressed:) forControlEvents:UIControlEventTouchUpInside];
    self.myCells = [self.myCells arrayByAddingObject:cell];
    [self.tableView reloadData]; // will call the delegate again and refresh cells
}
- (void)cellButton1Pressed:(id)sender {
    MyCell *cellPressed = nil;
    for(MyCell *cell in self.myCells) {
        if(cell.button1 == sender) {
            cellPressed = cell;
            break;
        }
    }
    // do whatever
}
- (void)cellButton2Pressed:(id)sender {
    MyCell *cellPressed = nil;
    for(MyCell *cell in self.myCells) {
        if(cell.button2 == sender) {
            cellPressed = cell;
            break;
        }
    }
    // do whatever
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.myCells.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.myCells[indexPath.row];
}

@end

【讨论】:

    猜你喜欢
    • 2013-05-10
    • 2020-10-20
    • 1970-01-01
    • 1970-01-01
    • 2015-09-17
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    • 1970-01-01
    相关资源
    最近更新 更多