Ios 在按钮上添加tableView作为子视图单击类外
我有一个带有自定义单元格的Ios 在按钮上添加tableView作为子视图单击类外,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个带有自定义单元格的tableViewController。守则如下: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section < 1) { static NSString *adicionaCellIdentifier = @"AdicionaCell";
tableViewController
。守则如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section < 1) {
static NSString *adicionaCellIdentifier = @"AdicionaCell";
AtividadesPraticadasCustomCell *adicionaCell = [tableView dequeueReusableCellWithIdentifier:adicionaCellIdentifier];
if (!adicionaCell)
{
adicionaCell = [[AtividadesPraticadasCustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"AdicionaCell"];
}
adicionaCell.backgroundColor = [UIColor blackColor];
adicionaCell.selectionStyle = UITableViewCellSelectionStyleNone;
NSLog(@"CELL1");
return adicionaCell;
}
else {
static NSString *atividadesCellIdentifier = @"AtividadesCell";
UITableViewCell *atividadesPraticadasCell = [tableView dequeueReusableCellWithIdentifier:atividadesCellIdentifier];
if (!atividadesPraticadasCell) {
atividadesPraticadasCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AtividadesCell"];
}
NSLog(@"%lu", (unsigned long)_arrayAtividadesPraticadas.count);
atividadesPraticadasCell.textLabel.text = [_arrayAtividadesPraticadas objectAtIndex:indexPath.row];
NSLog(@"CELL2");
return atividadesPraticadasCell;
}
}
当我在tableViewController的ViewDidLoad
或DidSelectRowatingIndexPath
中使用代码时,它工作正常
我还尝试在mainViewController
中创建一个方法,并从ui按钮调用该方法,但它也不起作用。当我在didSelectRowAtIndexPath
处调用该方法时,它会工作。我不确定我错过了什么
方法如下所示:
- (void)abrirTableViewParaAtividadesPraticadas
{
AtividadesPraticadasTableView *tableview = [[AtividadesPraticadasTableView alloc] initWithFrame:CGRectMake(10, 10, 300, 300) style:UITableViewStylePlain];
[self.view addSubview:tableview];
}
在此处添加按钮目标:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//init your custom cell here
[yourCustomCell.button addTarget:self action:@selector(botaoAdicionar:) forControlEvents:UIControlEventTouchUpInside];
}
然后在UITableViewController
中实现-(iAction)botaoAdicionar:(id)sender
方法
此外,如果要将UIViewController
或UITableViewController
添加为子视图,则必须按以下方式执行:
[destinationVC.view addSubview:tableViewController.view];
[destinationVC.view addSubview:tableViewController.view];