[英]can't click UIButton inside a cell in UITableview
已經搜索了一些可能的修復方法,但都沒有解決我的問題。
我一直點擊 uitableview 中的單元格而不是其中的按鈕。
這是我的代碼:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.backgroundColor = [UIColor blackColor];
UIView *v = nil;
NSArray *array = [cell subviews];
for (v in array) {
NSLog(@"%@",[v class]);
if( [v isKindOfClass:[UIView class]] ){
[v removeFromSuperview];
}
}
//tb
if (tableView == self.tb) {
v = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.tb.bounds.size.width, 120.0)];
if([feeds count]>0){
UIImageView *box=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"statusBox.png"]];
box.userInteractionEnabled = YES;
[v addSubview:box];
AsyncImageView *imageView1 = [[AsyncImageView alloc] initWithFrame:CGRectMake(10, 20.0f, 34.0f, 34.0f)];
imageView1.contentMode = UIViewContentModeScaleAspectFill;
imageView1.clipsToBounds = YES;
//cancel loading previous image for cell
[[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:imageView1];
if (users1 != nil && users1.imagelink != nil && (id) users1.imagelink != [NSNull null]){
imageView1.imageURL = [NSURL URLWithString:users1.imagelink];
}
else{
imageView1.image=[UIImage imageNamed:@"default_ProfilePic.png"];
}
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myFunction:)];
tapped.numberOfTapsRequired = 1;
[imageView1 addGestureRecognizer:tapped];
[tapped release];
imageView1.userInteractionEnabled = NO;
[v addSubview:imageView1];
UIFont *font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:11];
UILabel *descLabel1 = [[UILabel alloc] initWithFrame: CGRectMake(52, 23, 160, 48)];
descLabel1.text = [NSString stringWithFormat:@"%@ %@",users1.userfirstn,users1.userlastn];
descLabel1.textColor = [UIColor blackColor];
descLabel1.font = font;
descLabel1.adjustsFontSizeToFitWidth=YES;
descLabel1.numberOfLines = 0;
CGSize expectedLabelSize = [descLabel1.text sizeWithFont:descLabel1.font
constrainedToSize:descLabel1.frame.size
lineBreakMode:UILineBreakModeWordWrap];
CGRect newFrame = descLabel1.frame;
newFrame.size.height = expectedLabelSize.height;
descLabel1.frame = newFrame;
descLabel1.numberOfLines = 0;
descLabel1.userInteractionEnabled = NO;
[v addSubview:descLabel1];
UILabel *descLabel2= [[UILabel alloc] initWithFrame: CGRectMake(52, 43, 200, 48)];
StatusClass *stat1=[feeds objectAtIndex:indexPath.row];
descLabel2.text = [stat1 statcreate];
descLabel2.textColor = [UIColor blackColor];
descLabel2.font = font;
descLabel2.adjustsFontSizeToFitWidth=YES;
descLabel2.numberOfLines = 0;
CGSize expectedLabelSize2 = [descLabel2.text sizeWithFont:descLabel2.font
constrainedToSize:descLabel2.frame.size
lineBreakMode:UILineBreakModeWordWrap];
CGRect newFrame2 = descLabel2.frame;
newFrame2.size.height = expectedLabelSize2.height;
descLabel2.frame = newFrame2;
descLabel2.numberOfLines = 0;
descLabel2.userInteractionEnabled = NO;
[v addSubview:descLabel2];
UILabel *descLabel3= [[UILabel alloc] initWithFrame: CGRectMake(10, 63, 280, 80)];
StatusClass *stat=[feeds objectAtIndex:indexPath.row];
descLabel3.text = [stat stattext];
descLabel3.textColor = [UIColor blackColor];
descLabel3.font = font;
descLabel3.adjustsFontSizeToFitWidth=YES;
descLabel3.numberOfLines = 0;
descLabel3.userInteractionEnabled = NO;
[v addSubview:descLabel3];
//comment button
UIButton *buttonC = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonC addTarget:self action:@selector(sendComment:) forControlEvents:UIControlEventTouchUpInside];
[buttonC setImage:[UIImage imageNamed:@"comment.png"] forState:UIControlStateNormal];
buttonC.frame = CGRectMake(2, 160, 145, 35);
buttonC.userInteractionEnabled = YES;
[v addSubview:buttonC];
//share button
UIButton *buttonS = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonS.tag = indexPath.row;
[buttonS addTarget:self action:@selector(sendShare:) forControlEvents:UIControlEventTouchUpInside];
[buttonS setImage:[UIImage imageNamed:@"share.png"] forState:UIControlStateNormal];
buttonS.frame = CGRectMake(150, 160, 140, 35);
buttonS.userInteractionEnabled = YES;
[v addSubview:buttonS];
}
v.userInteractionEnabled = YES;
[cell addSubview:v];
}
return cell;
}
我還為按鈕嘗試了UITapGestureRecognizer
,但仍然無法正常工作。
謝謝。
我只是被這個問題弄糊塗了......
我通過在一個項目上執行此操作設法解決了這個問題:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("RegCell", forIndexPath: indexPath) as! CustomCell
cell.contentView.userInteractionEnabled = false // <<-- the solution
return cell
}
但同樣不適用於不同的項目。 我不知道為什么...
像這樣的問題還有另一個潛在原因,那就是UITableViewCells
現在有一個contentView
。 這可以並且將被放置在自定義表格視圖單元格中的其他視圖之前,以便拾取觸摸並檢測單元格選擇。 這樣做時,它可能會阻止對其背后的任何視圖的觸摸。 在使用筆尖創建單元格時,我特別注意到了這種煩惱。
處理這個問題的公認方法是確保所有子視圖都不是添加到 tableview 單元格本身,而是添加到它的contentView
。 這是一個只讀屬性,可在某些情況下調整自己的框架,例如在側向滑動以顯示刪除按鈕時,或在編輯模式期間。 因此,您應該確保添加到contentView
任何項目都經過智能調整大小並具有正確的調整大小行為。
另請注意,通過將單元格添加到contentView
,您自己可能會阻止對其進行觸摸,從而防止單元格被選中。 我個人盡量不允許在包含具有自定義用戶啟用控件的單元格的表格視圖中選擇單元格。 它只會導致混亂和尷尬的界面。 事實上,我正在認真考慮將我未來項目中的所有UITableViews
替換為UICollectionViews
,它們的適應性要強得多。
-灰
我是這樣做的:
let plusButton: UIButton = {
let v = UIButton()
v.setImage(plusImg, for: .normal)
v.tintColor = .dark
v.translatesAutoresizingMaskIntoConstraints = false
// adding targets here did not work
v.addTarget(self, action: #selector(plusButtonHandler), for: .touchUpInside)
return v
}()
並且在閉包中調用addTarget
方法似乎不起作用。
當我將addTarget
方法分解出來並將其放入初始化程序時,一切正常!
在我的情況下,我嘗試通過self.addSubview(myButton)
將 UIButton 直接添加到單元格,所以我更改為self.contentView.addSubview(myButton)
並且它起作用了
似乎contentView
在myButton
前面,所以按鈕不會收到點擊手勢。
您的UIButton
='sy 偏移量被指定為 160,而UIView's
高度僅被指定為 120。請更改UIButton's
y 偏移量並嘗試。
UITableViewCell 默認處理所有手勢。 設置cell.selectionStyle = UITableViewCellSelectionStyleNone;
使其禁用默認行為。
對於 Swift 3 和 Storyboard
對我來說,我必須在內容視圖中啟用多點觸控,並在所有內容上啟用用戶交互 - 表格視圖、表格視圖單元格、內容視圖和 UIButton。 我正在使用故事板。
我還將我的 tableview 單元格設置為子類。
由於我在探索問題時無法在這里找到答案,因此我在堆棧溢出上發布了另一個關於此問題的問題。 你可以在這里查看我的代碼並下載一個項目: Can't click button inside UITableViewCell?
我遇到了這個問題,但在我的情況下,它與表視圖無關,而是因為我在我的按鈕中使用了userInteractionEnabled
為 true 的子視圖,為所有按鈕的子視圖設置該值為 false (NO)我的問題。
我有同樣的問題,不知何故我發現了我的錯誤。
之后,我改變了constraints
與self(UITableViewCell)
,以constraints
與contentView
的self(UITableViewCell)
,我的手機的按鈕是點擊一次。
創建自定義 UITableViewCell。 請參閱此鏈接。
假設您有一個名為 displayButton 的UIButton
,您可以執行以下操作:
在您的- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
,在您創建單元格后,
[cell.dislayButton addTarget: self
action: @selector(replyButtonPressed:withEvent:)
forControlEvents: UIControlEventTouchUpInside];
並在您的 ViewController 中像這樣創建方法:
- (void) replyButtonPressed: (id) sender withEvent: (UIEvent *) event
{
UITouch * touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView: self.tableView];
NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint: location];
}
如果在某些UITableViewCell
內按下的UIButton
沒有替代選擇器,則需要關閉該按鈕的userInteractionEnabled
cellButton.userInteractionEnabled = false
在這種情況下,任何按鈕觸摸都會傳播到單元格觸摸。
試試這個
public override void SetSelected(bool selected, bool animated)
{
// this.interestsButton.Selected = true;
if (selected)
{
// this.TextLabel.TextColor = UIColor.Red;
//this.interestsButton.ShowsTouchWhenHighlighted = true;
this.interestsButton.BackgroundColor = UIColor.Purple;
//this.interestsButton.SetTitleColor(UIColor.Red, UIControlState.Highlighted);
}
else
{
}
// cell.ContentView.UserInteractionEnabled = false;
base.SetSelected(selected, animated);
}
僅在“Mvvmcross.iOS”上測試
只是添加到討論中:
對於 SWIFT 5+
在閉包內部調用addTarget
方法時,將變量設置為lazy var
變量很重要。 看:
lazy var plusButton: UIButton = {
let v = UIButton()
v.setImage(plusImg, for: .normal)
v.tintColor = .dark
v.translatesAutoresizingMaskIntoConstraints = false
v.addTarget(self, action: #selector(plusButtonHandler), for: .touchUpInside)
return v
}()
現在,關於出現在單元格上方的tableViewCells
的contentView
停止在單元格處單擊:我使用上面的代碼並將contentView
設置為.ishidden = true
並且它有效!
可能有一些與方法的生命周期相關聯的東西。
希望它也有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.