简体   繁体   中英

button action in UITableView Cell

In my app one UITableView is there. In table cell am placing two labels and one button. button height is equal to (label1+ label2) height. 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

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..

artistButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

after that you can initialize your UIButton code.

I suppose you do realize that the frames you set are overlapping. You do call [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:方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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