0

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

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

请帮忙

4

3 回答 3

0

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

于 2013-06-14T06:04:25.997 回答
0

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

  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:,所以也要定义它,否则会在按钮点击事件上出现崩溃

于 2013-06-14T05:49:26.203 回答
0

我认为问题在于

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

它会给你一个 UIButton 类型UIButtonTypeCustom,默认情况下是透明/清除颜色。

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

于 2013-06-14T06:18:21.843 回答