简体   繁体   English

在单个tableview中通过单击按钮逐个动态添加tableview单元格

[英]Dynamically add tableview cell one by one by button click in a single tableview

i. 一世。 I have tried a lot but could not execute successfully . 我已经尝试了很多,但是无法成功执行。 ii. ii。 In tableview cell, there are 3 3 fields need to display. 在tableview单元格中,需要显示3 3个字段。 One image view, button1 -->taking photo button, button2---> browse button. 一个图像视图,按钮1->拍照按钮,按钮2 --->浏览按钮。 iii. iii。 First time tableview should display a custom cell with one row. 第一次tableview应该显示一行的自定义单元格。 iv. iv。 When a user clicks "add new button" , which lays outside of tableview, a new row will create with all above 3 fields (image view, button1, button2) v. Number of clicks of "add new button" , will create new rows with all above 3 fields. 当用户单击位于表格视图之外的“添加新按钮”时,将创建一个新行,其中包含以上三个字段(图像视图,button1,button2)v。单击“添加新按钮”的次数将创建新行以上3个字段。 vi. vi。 I could do all above things created dynamically successfully with a simple image view which contains above 3 fields but could not succeed to work on custom cell. 我可以使用包含3个以上字段的简单图像视图成功完成动态创建的所有上述操作,但无法成功在自定义单元格上工作。

vii. 七。 Again I need to set the tag of each cell, broswe button, take photo button so that when clicks, will take the tag value. 再次,我需要设置每个单元格的标签,即broswe按钮,“拍照”按钮,以便在单击时获取标签值。

The table view works by adding a delegate and a data source. 表格视图通过添加委托和数据源来工作。 Assuming your table view has an owner as a view controller and both delegate and data source are the view controller itself. 假设您的表视图具有所有者作为视图控制器,并且委托和数据源都是视图控制器本身。 All you need to do is implement those data source methods to return an appropriate data then you should call reloadData on the table view or if you want a bit of extra work to look nicer check how to add rows animated around the web. 您需要做的就是实现这些数据源方法以返回适当的数据,然后应该在表视图上调用reloadData ,或者如果您想做一些额外的工作看起来更好,请检查如何在网络上添加动画行。

This is a very simple and not optimized example but is very short and easy to read. 这是一个非常简单且未优化的示例,但非常简短且易于阅读。 I hope it helps you get on the right track: 我希望它能帮助您走上正确的道路:

@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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM