我有一个loginViewController类,它有表内容。我已经将自定义单元格加载到表格的每一行。在方法cellForRowAtIndexPath中,我为每一行创建了一个按钮,并将其标记为一个标记值。单击此按钮将我们带到新的视图控制器logininsidesViewController。现在在logininsidesViewController类中,我想访问我之前创建的按钮标记。我该怎么办?
loginViewController.m class
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
customcell *cell=(customcell*)[tableView dequeueReusableCellWithIdentifier:@"cellcstm"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(customActionPressed)
forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[UIImage imageNamed:@"add_black.png"] forState:UIControlStateNormal];
button.frame = CGRectMake(275 ,9, 25, 25);
[cell addSubview:button];
if (cell==nil)
{
//[tableView registerNib:[UINib nibWithNibName:@"customcell" bundle:nil] forCellReuseIdentifier:@"cellcstm"];
NSArray *arr12=[[NSBundle mainBundle]loadNibNamed:@"customcell" owner:self options:nil];
cell=[arr12 objectAtIndex:0];
}
for (int i=0; i<=indexPath.row; i++)
{
cell.selectionStyle=UITableViewCellEditingStyleNone;
}
if (indexPath.row==0)
{
cell.lbl.hidden=YES;
UILabel *l=[[UILabel alloc]init];
l.text=@"Login Templates";
l.frame=CGRectMake(100, 15, 180, 28);
[cell.contentView addSubview:l];
}
if (indexPath.row==1)
{
button.tag=1;
cell.img.image=[UIImage imageNamed:@"facebook.png"];
cell.lbl.text=@"Facebook";
[cell.contentView addSubview:button];
}
if (indexPath.row==2)
{
button.tag=2;
cell.img.image=[UIImage imageNamed:@"g+.png"];
cell.lbl.text=@"Google";
[cell.contentView addSubview:button];
}
if (indexPath.row==3)
{
button.tag=3;
cell.img.image=[UIImage imageNamed:@"youtube.png"];
cell.lbl.text=@"Youtube";
[cell.contentView addSubview:button];
}
if (indexPath.row==4)
{
button.tag=4;
cell.img.image=[UIImage imageNamed:@"twitter.png"];
cell.lbl.text=@"Twitter";
[cell.contentView addSubview:button];
}
if (indexPath.row==5)
{
button.tag=5;
cell.img.image=[UIImage imageNamed:@"flicker.png"];
cell.lbl.text=@"Flicker";
[cell.contentView addSubview:button];
}
else
{
cell .textLabel.text= [logarr objectAtIndex:indexPath.row];
}
return cell;
}
我想访问logininsidesViewController类中的button.tag。请帮忙
答案 0 :(得分:1)
继续Viruss的回答,你可以在logininsidesViewController
类中创建一个整数类型的属性,并从按钮点击中传递它。
-(IBAction) customActionPressed:(id)sender{
NSLog(@"button tag:%d",[sender tag]);
logininsidesViewController *objLoginInside = [[logininsidesViewController alloc] initWithNibName:@"logininsidesViewController" bundle:nil];
objLoginInside.buttonTag = sender.tag;
[self.navigationController pushViewController:objLoginInside animated:NO];
}
答案 1 :(得分:0)
向按钮添加一个目标方法并使用标记
进行访问在cellForRow方法中更新为此。
[button addTarget:self action:@selector(customActionPressed:) forControlEvents:UIControlEventTouchUpInside];
您可以在此处使用标记
进行访问-(IBAction) customActionPressed:(id)sender{
NSLog(@"button tag:%d",[sender tag]);
}
答案 2 :(得分:0)
您要在单元格中添加button
,然后再将其添加到单元格contentView
上,这是正确的问题。
如果要在cell.contentView
中添加,则必须在每个if条件中提供分配,然后添加
修改:执行智能编码
viewDidLoad()
[self.myTableView registerNib:[UINib nibWithNibName:@"CViewCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
arImage = @[@"facebook.png", @"g+.png", @"twitter.png", @"flicker.png" ];
arLables = @[@"Facebook", @"GooglePlus", @"Tweeter", @"Flicker"];
cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell * cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(customActionPressed) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[UIImage imageNamed:@"add_black.png"] forState:UIControlStateNormal];
button.frame = CGRectMake(275 ,9, 25, 25);
if (indexPath.row==0)
{
cell.lblText1.hidden=YES;
UILabel *l=[[UILabel alloc]init];
l.text=@"Login Templates";
l.frame=CGRectMake(100, 15, 180, 28);
[cell.contentView addSubview:l];
}
if ( indexPath.row > 0 && indexPath.row <= 5) {
button.tag = indexPath.row;
cell.imageView.image=[UIImage imageNamed:arImage[indexPath.row - 1]];
cell.textLabel.text=[arLables objectAtIndex:indexPath.row - 1];
[cell.contentView addSubview:button];
} else {
cell.textLabel.text= [logarr objectAtIndex:indexPath.row];
}
return cell;
}