2

私が持っているのは、別の で作成されたカスタム テーブル セルです.xib。それに、このための Objective-C クラスがあります。カスタム セルのラベルをカスタム セルのクラスに接続しました。

メインの .xib にはTableView、カスタム セルが追加された .xib があります。データの取り込みを行うコードは、メイン クラス (ViewController.m) にあります。

labelでは、メイン クラス (ViewController.m) からカスタム セルを変更するにはどうすればよいでしょうか。

ユーザーがカスタム セルをタップすると、ダイアログが表示labelされ、ダイアログで選択されたボタンに応じて、カスタム セル内のテキストが変更されます。

4

4 に答える 4

2

これはテーブル セルなので、テーブル ビュー内で使用することを前提としています。通常は、

- (UITableViewCell *)UITableView:(UITableView *) cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"myCustomCell";
    MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyCustomCell" owner:self options:nil];
        for (id anObject in nib) {
            if ([anObject isKindOfClass:[MyCustomCell class]]) {
                cell = (MyCustomCell *)anObject;
            }
        }
    }
    cell.myLabel.text = @"Some Text"; // This will set myLabel text to "Some Text"
    return cell;
}
于 2013-01-05T02:22:01.573 に答える
1

ラベルにタグを割り当てる必要があります。つまり、Interface builder では 99 です。次に、ViewController.m で、セルを読み込んでいるときに、xib を読み込んだ後に実行できます

UILabel *label = [cell viewWithTag: 99];
label.text = @"Some text here... (:";

それはトリックを行います!:)

于 2013-01-05T02:18:44.983 に答える
1

とてもシンプルです:

最初にカスタムセルにラベルのプロパティを作成します

カスタムセル.h

@interface CustomCell : UITableViewCell 

@property (strong, nonatomic) IBOutlet UILabel *yourLabel;

TableViewController に customcell のインスタンスを作成します

YourTableViewController.h
@interface YourTableViewController : UITableViewController<

    {
        CustomCell *cell;
    }

および YourTableViewController.m で

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

    cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:nil];

    if (cell == nil) 
    {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];

        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell =  (CustomCell *) currentObject;
                break;

            }
        }
    }

cell.yourLabel.text = @"whatever you want to add";

今、これを行う以外の方法でcustomcellのラベルを更新したい場合。

-(void)someMethod()
{
CustomCell *acell = (CustomCell *)[tableView cellForRowAtIndexPath:n];
acell.yourLabel.text = @"whatever you want to add.";
}
于 2013-01-05T07:49:27.750 に答える
0

私は別のアプローチを考え出し、現在、私はそれが嫌いなので、iOS開発を停止しています(笑)

とにかく、すぐに返信してくれてありがとう。

于 2013-01-20T00:43:18.430 に答える