【问题标题】:selector for multiple uibuttons in uitableviewcelluitableviewcell 中多个 uibutton 的选择器
【发布时间】:2015-10-11 12:48:55
【问题描述】:

我正在为多个UIButtons 添加相同的选择器,它们是UITableViewCell 的一部分,它仅适用于第一个。我错过了什么吗?

这是我的代码:

- (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");
}

【问题讨论】:

  • 您是否尝试在选择器上设置断点?你能提供它的代码吗?
  • 所有这些都在 willDisplayCell 中,而不是在 cellForRowAtIndexPath 中?你不希望这个单元格被重用后会出现一些奇怪的问题吗?
  • 是的,请分享self answererd:的代码
  • 我很确定答案:没有调用按钮(除了第一个 ofc),因为我把日志记录在那里
  • 当您点击未调用您的已回答方法的按钮时,您能否判断该按钮是否实际被按下? (点击时文本颜色会改变吗?)如果按钮在其父级的边界之外,它将无法点击,即使它可能呈现得很好(剪裁关闭)。

标签: ios objective-c cocoa-touch uibutton selector


【解决方案1】:

好的,我找到了。非常感谢TomSwift,因为他的观点让我想到了在所有视图上放置边框以调试它们的帧大小和来源。

请注意,我为按钮提供了与其超级视图相同的框架,这是错误的,因为它们不应该具有相同的来源。按钮的原点应为 0,0,因为它们的帧大小与其父视图相同。

为像我这样的菜鸟学习:

  • 当按钮不起作用时,放置边框以确保其框架在超级视图的范围内。
  • 不要在视图及其子视图上分配相同的框架。它会将其置于 superview 的范围之外,这在 92% 的情况下会带来不良行为。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-18
    • 1970-01-01
    相关资源
    最近更新 更多