0

i have implemented this code, its showing both labels, but button not coming in cell tell me the issue

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

please help for this

4

3 に答える 3

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)];

UIButtonTypeCustomデフォルトで透明/クリアカラーになるタイプのUIButtonを返します。

セルに追加されているが表示されていない可能性があります。ボタンの背景色を変更してみて、まだ表示されていないかどうかを確認してください。

于 2013-06-14T06:18:21.843 に答える