1

タイトルと詳細という名前のテーブルビューセルに表示するために手動で作成した2つのラベルがあります。表示するコードは、

dealarray = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",nil];

detailarray = [[NSMutableArray alloc]initWithObjects:@"oneoneoneoneone oneoneoneoneoneoneoooooooo",@"two",@"three",@"fouronefouronefouronefouronefouronefouronefouron",nil];

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
            for(UILabel *lbl in [cell.contentView subviews])
        {

            [lbl removeFromSuperview];


        }
        cell.accessoryType= UITableViewCellAccessoryNone;
        UILabel* title;

        title= [[UILabel alloc] initWithFrame:CGRectMake(5,5,300,20)];
        [cell.contentView addSubview:title];
        [cell.contentView bringSubviewToFront:title];
        [title setFont:[UIFont boldSystemFontOfSize:14]];
        title.tag = 1001;
        title.backgroundColor = [UIColor clearColor];

        title.textColor = [UIColor blackColor];
        title.text =[dealarray objectAtIndex:indexPath.row];

        UILabel* detail;
        detail= [[UILabel alloc] initWithFrame:CGRectMake(5,30,300,10)];
        [cell.contentView addSubview:detail];
        [cell.contentView bringSubviewToFront:detail];
        [detail setFont:[UIFont systemFontOfSize:12]];
        detail.tag = 1002;
        detail.backgroundColor = [UIColor clearColor];

        detail.textColor = [UIColor blackColor];

        detail.text = [detailarray objectAtIndex:indexPath.row];

  return cell
 }

これらの2つのラベルを表示しても問題はなく、すべての「詳細」ラベルを非表示にして「タイトル」のみを表示しても問題ありません。セルの選択的な「詳細」ラベルを表示しようとすると問題が発生します。

試したコード:

 // conti of cellforrowatindexpath
 detail.numberOfLines = 3;
   detail.lineBreakMode = NSLineBreakByWordWrapping;

    if (a==-1)// declared 'a' in viewdidload as -1
      {
       ((UILabel*)detail).hidden = YES;
      }
    else if(a==indexPath.row)
       {
         ((UILabel*)detail).hidden = NO;
        }
       ((UILabel*)detail).hidden = YES;



return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 [tableView deselectRowAtIndexPath:indexPath animated:YES];
    a=indexPath.row;

[tableview reloadData];


 }

大量のコードを投稿して申し訳ありませんが、私の疑いのある健全なデータを探している人に役立つかもしれません.

何が間違っているのか、セルを選択するための詳細ラベルを非表示にすることはできません。この点で誰か助けてもらえますか?

4

2 に答える 2

0

ロジックにエラーがあります。書かれているようにコーディングすると、detail.hidden は常に YES に設定され、前の 2 つの if は無視されるため、次のようになります (ところで、型強制と追加の括弧は必要ありません)。

if(a==-1) detail.hidden = YES;
そうでなければ (a==indexPath.row) detail.hidden = NO;
他の詳細。非表示 = YES;

于 2014-02-01T15:38:31.857 に答える
0

xcode 機能を使用してセル コンテンツを設計することをお勧めしますか? (たとえば、セルにUILabelをドラッグアンドドロップするのと同じくらい簡単です)次に、それぞれの「タグ」IDを使用してコードからそれらにアクセスできます。正しい方法の詳細は次のとおりです。http://www.appcoda.com/customize-table-view-cells-for-uitableview/ xcode インターフェイスを使用すると、セルのコンテンツをプログラムで作成するよりも優れた方法だと思います。インターフェイスを完璧に定義することができます。

とにかく、質問に正確に答えるには、「didSelectRowAtIndexPath」から詳細ラベルを非表示にする必要があります。

// Retrieve the corresponding cell
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

// Get the detail label (using its tag) you set it to 1002
UILabel *detail = (UILabel *)[cell viewWithTag:1002];

// Hide it
detail.hidden = true;

この助けを願っています。

于 2013-09-06T11:00:10.630 に答える