繁体   English   中英

UITableView Cell中的按钮操作

[英]button action in UITableView Cell

在我的应用程序中,有一个UITableView。 在表格单元格中放置两个标签和一个按钮。 按钮高度等于(label1 + label2)高度。 这些是显示内容。 和按钮是为了行动。 这里我的问题是无法在所有部分执行按钮操作。 点击时只有部分工作正常。

这是用于放置标签和按钮的代码

artistButton = [[UIButton alloc] initWithFrame:CGRectMake(11.0, 6.0, 170.0, 50.0)];
artistButton.backgroundColor = [UIColor redColor];
[self.contentView bringSubviewToFront:artistButton];
[self.contentView addSubview:artistButton];

artistLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(11.0, 6.0, 170.0, 27.0)];
artistLabel1.textAlignment = UITextAlignmentLeft;
artistLabel1.textColor = [UIColor colorWithRed:0.14 green:0.29 blue:0.42 alpha:1]; 
artistLabel1.font =  [UIFont boldSystemFontOfSize:15.0];
artistLabel1.backgroundColor = [UIColor blueColor];
[self.contentView addSubview:artistLabel1];

artistLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(11.0,20.0, 170.0, 27.0)];
artistLabel2.textAlignment = UITextAlignmentLeft;
artistLabel2.textColor = [UIColor grayColor];
artistLabel2.font =  [UIFont boldSystemFontOfSize:12.0];
artistLabel2.backgroundColor = [UIColor grayColor];
[self.contentView addSubview:artistLabel2];

任何人都可以提供帮助或建议。 提前致谢。

我找到了问题的答案。 由于按钮和标签的大小,它只是不起作用。 现在调整按钮的宽度和长度,并标记其工作正常。 谢谢你们。

将Target操作添加到此Button

artistButton = [[UIButton alloc] initWithFrame:CGRectMake(11.0, 6.0, 170.0, 50.0)];
artistButton.backgroundColor = [UIColor redColor];

[artistButton addTarget:self 
           action:@selector(ArtistAction)
 forControlEvents:UIControlEventTouchUpInSide];

[self.contentView bringSubviewToFront:artistButton];
[self.contentView addSubview:artistButton];


-(void)ArtistAction
{
  NSLog("Test Artist Action");
}

在UIButton声明之前添加一行..

artistButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

之后,您可以初始化您的UIButton代码。

我想你确实意识到你设置的帧是重叠的。 你可以调用[self.contentView bringSubviewToFront:artistButton]; 但是你之后添加按钮作为子视图它将没有任何效果。 您还可以在添加按钮后添加其他子视图,这些子视图将放置在您的按钮上方。

因此,首先添加两个标签,然后按钮,您应该能够正确地单击按钮,但标签将位于按钮下方,并且不确定它们中有多少可见。 检查要为要添加的对象设置的帧。

只需使用你的cell tableView:didSelectRowAtIndex:方法。

暂无
暂无

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

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