2

背景はほとんどありませんが、テーブルビューは、テーブルビューを文字列で埋めるfetchedResultsControllerによって埋められます。次に、各テーブルビューセルの各文字列の横にボタンを追加しようとしています。これまで、configureCell:atIndexPathメソッドでボタンを作成し、そのボタンをサブビューとしてテーブルセルのコンテンツビューに追加しようとしましたが、何らかの理由でボタンが表示されません。以下は関連するコードです。誰かがもっと多くのコードを投稿したいのなら、ただ尋ねてください、そして私はあなたが望むものを何でも載せます。どんな助けでも大歓迎です。

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {

// get object that has the string that will be put in the table cell
 Task *task = [fetchedResultsController objectAtIndexPath:indexPath];

 //make button
 UIButton *button = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
 [button setTitle:@"Check" forState:UIControlStateNormal];
 [button setTitle:@"Checked" forState:UIControlStateHighlighted];

 //set the table cell text equal to the object's property
 cell.textLabel.text = [task taskName];

 //addbutton as a subview
 [cell.contentView addSubview:button];
 }

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

 static NSString *CellIdentifier = @"Cell";

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

 // Configure the cell.
 [self configureCell:cell atIndexPath:indexPath];
    return cell;
}
4

1 に答える 1

2

3つのコメント:

  1. おそらく、のフレームを設定する必要がありますUIButton。そうでなければ、それがどれくらいの大きさで、どこに配置されるのかわかりません。デフォルトのフレームが付属している可能性がありますが、私はそれを調査していません。
  2. あなたはあなたのボタンを漏らしています。retain電話した後は必要ありません[UIButton buttonWithType:...];
  3. 同じセルに複数のボタンを追加する可能性があります。セルを再利用する場合は、最初removeFromSuperviewにの各サブビューを呼び出す必要がありcell.contentViewます。
于 2010-05-14T17:28:45.570 に答える