4

CustomCell を使用した UITableView があります。カスタム セルには、UILabel と UIImageView が含まれています。

ユーザーがセルを押したときに、通常の UITableView セルからテキストを取得し、文字列に格納できることをオンラインで見ました。

しかし、カスタム セルを使用している場合、どうすればこれを行うことができるでしょうか? UILabel の名前は「type_label」で、CustomCell XIB のヘッダー ファイルで定義されているためです。

したがって、Xcodeでは使用できません:

cell.type_label.text"

次の関数では:

-(void)tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

ありがとう、ダン。

4

2 に答える 2

12

tableViewDidSelectRow で行う必要があるのは、次のとおりです。

UITableViewCellCustomClass *customCell = (UITableViewVellCustomClass*)[tableView cellForRowAtIndexPath:indexPath];

NSString *labelText = [[customCell type_label] text];

それはあなたが必要とするようになるはずです。

于 2013-09-04T16:16:05.250 に答える
-1

以下のコードを試してください - CCustomCellClass に type_label ラベルの IBOutlet を作成しました
(file-->new-->subclass--> UITableViewCell-->CustomCellClass のように名前を付けます)
次に、以下のコードを実装します

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

static NSString *cellIdentifier = @"Cell";

static BOOL nibsRegistered = NO;

if (!nibsRegistered) {

    UINib *nib = [UINib nibWithNibName:@"CustomCellClass" bundle:nil];
    [tableView registerNib:nib forCellReuseIdentifier:cellIdentifier];
}

CustomCellClass *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil) {

    cell = [[CustomCellClass alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

cell.type_label.text = @"Rocky"; // setting custom cell label

return  cell;

}
于 2013-09-04T16:58:56.687 に答える