一世。我已经尝试了很多,但是无法成功执行。
ii。在tableview单元格中,需要显示3 3个字段。一个图像视图,按钮1->拍照按钮,按钮2 --->浏览按钮。
iii。第一次tableview应该显示具有一行的自定义单元格。
iv。当用户单击位于表格视图之外的“添加新按钮”时,将创建一个新行,其中包含以上3个字段(图像视图,button1,button2)
v。单击“添加新按钮”的次数,将创建包含以上3个字段的所有新行。
vi。我可以使用包含以上3个字段的简单图像视图成功完成动态创建的所有上述操作,但无法成功在自定义单元格上工作。

七。同样,我需要设置每个单元格的标签,即broswe按钮,“拍照”按钮,以便在单击时将获取标签值。

最佳答案

表格视图通过添加委托和数据源来工作。假设您的表视图具有所有者作为视图控制器,并且委托和数据源都是视图控制器本身。您需要做的就是实现这些数据源方法以返回适当的数据,然后应该在表视图上调用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

10-05 20:21
查看更多