简体   繁体   English

UITableView Cell中的按钮操作

[英]button action in UITableView Cell

In my app one UITableView is there. 在我的应用程序中,有一个UITableView。 In table cell am placing two labels and one button. 在表格单元格中放置两个标签和一个按钮。 button height is equal to (label1+ label2) height. 按钮高度等于(label1 + label2)高度。 these are to display content. 这些是显示内容。 and button is for action. 和按钮是为了行动。 Here my problem is am not able to perform button action in all portion. 这里我的问题是无法在所有部分执行按钮操作。 only some part is working when clicking. 点击时只有部分工作正常。

Here is the code am used to place the labels and buttons 这是用于放置标签和按钮的代码

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];

any one can help or suggest. 任何人都可以提供帮助或建议。 Thanks in advance. 提前致谢。

I found answer for my question. 我找到了问题的答案。 Because of size of the button and labels only it is not working. 由于按钮和标签的大小,它只是不起作用。 Now am adjusted the width and length of button and labels its working fine. 现在调整按钮的宽度和长度,并标记其工作正常。 Thank you all. 谢谢你们。

Add Target action to this Button 将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");
}

add one line before UIButton declaration.. 在UIButton声明之前添加一行..

artistButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

after that you can initialize your UIButton code. 之后,您可以初始化您的UIButton代码。

I suppose you do realize that the frames you set are overlapping. 我想你确实意识到你设置的帧是重叠的。 You do call [self.contentView bringSubviewToFront:artistButton]; 你可以调用[self.contentView bringSubviewToFront:artistButton]; but you add the button as a subview after so it will have no effect. 但是你之后添加按钮作为子视图它将没有任何效果。 Also you add other subviews after adding the button and those will be placed above you button. 您还可以在添加按钮后添加其他子视图,这些子视图将放置在您的按钮上方。

So just add the two labels first and then the button you should be able to click the button properly, though you labels will be under the button and not sure how much of them will be visible. 因此,首先添加两个标签,然后按钮,您应该能够正确地单击按钮,但标签将位于按钮下方,并且不确定它们中有多少可见。 Check the frames you are setting for the objects you are adding. 检查要为要添加的对象设置的帧。

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

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

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