2015-07-21 16 views
0

我爲UITableViewCell中的多個UIButtons添加了相同的選擇器,它僅適用於第一個。我錯過了什麼嗎?用於uitableviewcell中多個uibutton的選擇器

這裏是我的代碼:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{ 

    NSDictionary *d = [tableData objectAtIndex:indexPath.row]; 
    NSArray *answers = [d objectForKey:@"answers"];//array of { id = 35; text = "\U03c4\U03a\U03c0\U03bf\U03c4\U03b1"; } 

    UILabel *startDate = (UILabel *)[cell viewWithTag:100]; 
    long long startDateEpoch = [[d objectForKey:@"startDate"] longLongValue]; 
    NSDate *sDate = [NSDate dateWithTimeIntervalSince1970:startDateEpoch/1000]; 
    NSDateFormatter *startDateFormatter = [[NSDateFormatter alloc] init]; 
    [startDateFormatter setDateFormat:@"dd/MM/yyyy"]; 

    startDate.text = [startDateFormatter stringFromDate:sDate]; 

    ((UILabel *)[cell viewWithTag:101]).text = [d objectForKey:@"subject"]; 

    NSArray *tags = [d objectForKey:@"tags"]; 
    ((UILabel *)[cell viewWithTag:102]).text = [tags componentsJoinedByString:@" "]; 

    NSString *imageURL = [d objectForKey:@"media"]; 
    UIImageView *iv = (UIImageView *)[cell viewWithTag:103]; 
    iv.contentMode = UIViewContentModeScaleAspectFit; 
    [iv sd_setImageWithURL:[NSURL URLWithString:imageURL] 
      placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 

    UIView *buttonsContainer = (UIView *)[cell viewWithTag:104]; 
    for(UIView *vv in buttonsContainer.subviews){ 
     [vv removeFromSuperview]; 
    } 
    int index = 0; 
    NSLog(@"buttonsContainer children: %d", buttonsContainer.subviews.count); 
    for(NSDictionary *answer in answers){ 
     UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, index * 40 + (index+1)*5, [UIScreen mainScreen].bounds.size.width, 40)]; 
     v.backgroundColor = [UIColor whiteColor]; 
     CGRect labelFrame = v.frame; 
     labelFrame.origin.y = 0; 
     UILabel *l = [[UILabel alloc] initWithFrame:labelFrame]; 
     l.text = (NSString *)[answer objectForKey:@"text"]; 
     l.textAlignment = NSTextAlignmentCenter; 

     UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, v.frame.size.height - 1, v.frame.size.width, 1)]; 
     bottomLine.backgroundColor = [UIColor colorWithRed:238.0/255.0 green:205.0/255.0 blue:103.0/255.0 alpha:1.0]; 

     UIButton *b = [[UIButton alloc] initWithFrame:v.frame]; 

     [b addTarget:self action:@selector(answered:) forControlEvents:UIControlEventTouchUpInside]; 

     [v addSubview:l]; 
     [v addSubview:bottomLine]; 
     [v addSubview:b]; 
     [buttonsContainer addSubview:v]; 

     index++; 
    } 
} 

因此,例如,如果添加了4個按鈕,選擇只有當我點擊第一個調用。當我點擊其他三個時候沒有任何反應。

編輯: 添加answered:代碼:

- (void)answered:(UIButton *)sender 
{ 
    NSLog(@"@answered"); 
} 
+0

您是否嘗試在選擇器上設置斷點?你能提供它的代碼嗎? – Astoria

+0

所有這一切都在willDisplayCell而不是cellForRowAtIndexPath? 只要這個單元格被重用,你不期待一些奇怪的問題嗎? –

+0

是的,請分享'self answererd:' –

回答

0

好吧,我發現它。許多人對TomSwift表示讚賞,因爲他的觀點讓我有想在所有視圖上放置邊界以調試幀大小和原點的想法。

請注意,我給我的按鈕作爲他們的superviews相同的框架,這是錯誤的,因爲他們不應該有相同的起源。按鈕的原點應該是0,0,因爲它們的大小與其超級視圖相同。

教訓菜鳥對於像我這樣的:

  • 當一個按鈕不起作用,邊境的地方,以確保他們的框架是上海華盈的範圍內。
  • 請勿在視圖及其子視圖上分配相同的幀。它會將它置於超級觀點的界限之外,其中92%的案例帶來不良行爲。