4

内部にボタンがあるため、カスタムセルを変更する解決策が見つかりません。ボタンをクリックしたときにラベルのテキストを変更したいと思います。

- (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:nil];
}

// View
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 250)];
[view setBackgroundColor:[UIColor colorWithWhite:255.0 alpha:0.0]];

// Label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320-7-7, 16)];
[label setText:@"Hello"];
[label setTextAlignment:UITextAlignmentLeft];
[label setTextColor:[UIColor grayColor]];
[label setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:16]];
[label setBackgroundColor:[UIColor clearColor]];
[view addSubview:label];

// Action
UIButton *action = [[UIButton alloc] initWithFrame:CGRectMake(306-54, 0, 54, 54)];
[action setTitle:@"0" forState:UIControlStateNormal];
[action addTarget:self action:@selector(actionMore:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:action];

// Add view to cell
[cell addSubview:view];

return cell;
}

編集(詳細を追加)

- (void)actionMore:(id)sender{
UIButton *button = (UIButton *)sender;

// Edit current cell, but I do not know how...

}

ありがとう。

4

5 に答える 5

4

次のコード行で、ボタンが押されたセルを取得できます

UITableViewCell * cell = (UITableViewCell*)[[sender superview] superview];

次に、セル参照を使用してラベル テキストを変更します

cell.yourlabel.text = @"新しいテキスト";

更新 上記のコードは iOS 7 では動作しない可能性があると思います。より良い方法は

CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.mainTable];
NSIndexPath *indexPath = [self.mainTable indexPathForRowAtPoint:buttonPosition];
于 2012-08-13T16:38:55.897 に答える
1

UIButtonUILabelは同じビューのサブビューであるため、ラベルにタグを付けて、ボタンのアクション コードからラベルを見つけるために使用できますviewWithTag

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320-7-7, 16)];
label.tag = 1234;
....

- (void)actionMore:(id)sender{
    UIButton *button = (UIButton *)sender;
    UILabel *label = [[sender superview] viewWithTag:1234];
    // Edit current cell
}
于 2012-08-13T16:39:31.177 に答える
1

あなたを助けるために、いくつかの詳細を提供する必要があります。

  1. これは単一のセルですか、それとも複数のプロトタイプですか?
  2. actionMore: セレクターのコード。
  3. 何のためにこれをしようとしているのですか?ラベルのテキストを変更するセル内のボタンはなぜですか?

このセルが複数回使用されている場合、問題は actionMore である可能性があります。どのセルがコードのターゲットであるかを特定できません。また、actionMore メソッドに単純なタイプミスがある可能性もありますが、相互作用するすべてのコードを確認できなければ、それを解決することはできません。

詳細を提供できる場合は、誰かが助けることができるかもしれません. さらに情報が得られたら、この回答を編集します。

- 編集 -

最初に使用できるセルを取得します。

UITableViewCell * cell = (UITableViewCell*)[[sender superview] superview];

次に、次の方法でセルのラベルにアクセスできます。

cell.label.text = @"Some Text Here";

次の問題は、セルがリスト内のどこにあるかを把握することです。そのためには、次のコードを使用します。

UITableView * table = (UITableView*)[[[sender superview] superview] superview];
NSIndexPath * indexPath = [table indexPathForCell: cell];

その後、switch ステートメントまたは if-else ステートメントを使用して、トリガーされた行を見つけることができます。

于 2012-08-13T16:20:14.087 に答える
0
- (void)actionMore:(id)sender{
    UITableViewCell * cell = (UITableViewCell*)[[sender superview] superview];

    UIView *view = [cell.subviews objectAtIndex:0];
    UILabel *label = [view.subviews objectAtIndex:0];

    [label setText:@"test"];
}
于 2012-08-13T18:46:05.917 に答える
0

の_ view_subviewcontentview

を渡すか、そこからフェッチしますindexpathactionMoreを取得し indexpathたら、ビジネスを開始します。

textlabelから取得するためのタグを設定することができますsuperview

于 2012-08-13T16:43:03.383 に答える