【问题标题】:UITable view not displaying button in cell iOSUITableview没有在单元格iOS中显示按钮
【发布时间】:2013-06-14 05:38:31
【问题描述】:

我已经实现了这段代码,它显示了两个标签,但是按钮没有出现在单元格中告诉我问题

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {

     static NSString *CellIdentifier = @"ImageOnRightCell";

     UILabel *mainLabel, *secondLabel;
     UIButton *buttonMe;

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

        mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 220.0, 15.0)];
        mainLabel.tag = MAINLABEL_TAG;
        mainLabel.font = [UIFont systemFontOfSize:14.0];
        mainLabel.textAlignment = UITextAlignmentRight;
        mainLabel.textColor = [UIColor blackColor];
        mainLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
        [cell.contentView addSubview:mainLabel];

        secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 20.0, 220.0, 25.0)];
        secondLabel.tag = SECONDLABEL_TAG;
        secondLabel.font = [UIFont systemFontOfSize:12.0];
        secondLabel.textAlignment = UITextAlignmentRight;
        secondLabel.textColor = [UIColor darkGrayColor];
        secondLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
        [cell.contentView addSubview:secondLabel];

        buttonMe = [[UIButton alloc] initWithFrame:CGRectMake(260.0, 7.0, 30.0, 30.0)];
        buttonMe.tag = PHOTO_TAG;
        buttonMe.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
        [cell.contentView addSubview:buttonMe];
   }
   else {
       mainLabel = (UILabel *)[cell.contentView viewWithTag:MAINLABEL_TAG];
       secondLabel = (UILabel *)[cell.contentView viewWithTag:SECONDLABEL_TAG];
       buttonMe = (UIButton *)[cell.contentView viewWithTag:PHOTO_TAG];
   }

   mainLabel.text = @"main text";
   secondLabel.text = @"Secon text";
   buttonMe.titleLabel.text=@"oh";

   return cell;
}  

请帮忙

【问题讨论】:

  • 您的单元格尺寸是多少?当您添加带有 X: 260 的按钮时,您可以检查它是否超出单元格的矩形范围吗?
  • 我也尝试过 axter settin x=200 或其他不工作的值
  • stackoverflow.com/questions/8633824/… 打开上面的链接...可能它会为你 kritika 使用完整...

标签: ios uitableview customization


【解决方案1】:

我认为问题出在

    buttonMe = [[UIButton alloc] initWithFrame:CGRectMake(260.0, 7.0, 30.0, 30.0)];

它会返回一个UIButtonTypeCustom 类型的 UIButton,默认为透明/透明颜色。

所以可能是它被添加到单元格但不可见,尝试更改按钮的背景颜色并检查它是否仍然不可见。

【讨论】:

  • 它现在来了,我有 4 个部分和多行部分,我在每行添加按钮,它应该作为单独的,我如何分配标签值
  • 您应该将其作为一个新问题提出。但无论如何,一种设置标签的解决方案如下:[button setTag:((indexPath.section & 0xFFFF) > 16) & 0xFFFF); NSUInteger 行 = (sender.tag & 0xFFFF);
  • 另外,如果您需要可以使用的单元格UITableViewCell* cell = [button superview];,请参考我的其他答案link
  • 请您向我解释一下..((indexPath.section & 0xFFFF)
  • 你不明白什么?? ,在提出新问题之前,您还应该接受一个解决了您之前问题的答案。
【解决方案2】:

要以编程方式创建按钮,请使用此代码

  UIButton *buttonMe = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [buttonMe addTarget:self
               action:@selector(aMethod:)
     forControlEvents:UIControlEventTouchDown];
    [buttonMe setTitle:@"Show View" forState:UIControlStateNormal];
    buttonMe.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
    buttonMe.tag = PHOTO_TAG;

    buttonMe.frame = CGRectMake(260.0, 7.0, 30.0, 30.0);
    [cell.contentView addSubview:buttonMe];

注意我也包含了设置动作的代码。当你点击它时会尝试aMethod:的方法,所以也要定义它,否则按钮点击事件会出现崩溃

【讨论】:

  • [cell.contentView addSubview:buttonMe],我不应该使用这个
  • 根据您的要求编辑。使用此
  • 我已经修改了代码。试试吧,让我知道它是否有效
  • 还没来,可能是其他问题
  • 所有标签都可见?如果是,请尝试代码以及​​标签的框架。尝试不使用自动调整代码
【解决方案3】:

试试这个,为 buttonMe 设置 backgroundColor:buttonMe.backgroundColor = [UIColor grayColor]; 并将 "buttonMe.titleLabel.text=@"oh"" 更改为 "[buttonMe setTitle:@"oh" forState:UIControlStateNormal];"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 2015-03-13
    • 1970-01-01
    相关资源
    最近更新 更多