1

編集:この投稿はちょっと長いので、最後の質問はここにコピーされます:

つまり、明らかに、クリックしているセルをチェックしているのではなく、作成している新しいセルをチェックしているのです。クリックされたセルをチェックし、その特定のセルにあるコンテンツをチェックする(つまり、そのセルのラベルのテキストをチェックする)にはどうすればよいでしょうか。

したがって、InterfaceBuilderにはプロトタイプのセルがあり、次のコードを実行して、uitableviewで多数のセルを作成します。

//tableview datasource delegate methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return cellIconNames.count;
}

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

static NSString *CellIdentifier = @"Cell";

cellIconName = [cellIconNames objectAtIndex:indexPath.row];


NSString *cellIconImageName = [[self cellIconImages] objectAtIndex:[indexPath row]];

CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];


if(cell == nil){
    cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.rightLabel.text = [cellIconNames objectAtIndex:indexPath.row];
cell.carrierImage.image = [UIImage imageNamed:cellIconImageName];

urlString = [cellButtons objectAtIndex:indexPath.row];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 290, 88);
[button setTitle:@"" forState:UIControlStateNormal];
[button addTarget:self action:@selector(startCarrierSite:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
[cell.contentView bringSubviewToFront:button];

return cell;
}

つまり、基本的には、各セルに画像ビュー、ラベル、シースルーボタンを作成し、それぞれの画像とラベルに配列に従って異なることを言わせるようにしました。

各セルにあるボタンは、同じセレクターstartCarrierSiteを指しています。

これはstartCarrierSiteのメソッドです

-(IBAction)startCarrierSite:(CustomCell *)cell;{

UILabel *rightLabel = (UILabel*) [cell.subviews objectAtIndex:0];

NSString *labelText = rightLabel.text;

if(labelText == @"Aetna"){
    NSURL *url = [NSURL URLWithString:@"www.aetna.com"];
    [[UIApplication sharedApplication] openURL:url];
}else{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"www.google.com"]];
    }
}

私がやろうとしているのは、セルのラベルを確認することです。そのラベルの.textが特定の文字列と等しい場合は、safariとblahblahblahを開くためのコードを実行します。これは問題ではありません。

問題は、セル内のラベルを確認できないように見えることです。私がこれを行おうとしている現在の方法は、CustomCell(UITableViewCellクラスの子)の引数を使用してアクションを実行することです。

これは機能し、クリックするとセルがチェックされますが、ラベルのテキストをチェックするとクラッシュします。エラーが発生します

キャッチされなかった例外'NSRangeException'が原因でアプリを終了しています、理由:' * -[__ NSArrayI objectAtIndex:]:インデックス4294967295が空の配列の境界を超えています'

つまり、明らかに、クリックしているセルをチェックしているのではなく、作成している新しいセルをチェックしているのです。クリックされたセルをチェックし、その特定のセルにあるコンテンツをチェックする(つまり、そのセルのラベルのテキストをチェックする)にはどうすればよいでしょうか。

編集(更新されたメソッドの追加):

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

CustomCell *cell = (CustomCell*)[tableView cellForRowAtIndexPath:indexPath];

if ([cell.urlString isEqualToString:@"Aetna"]) {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"www.aetna.com"]];
}else if([cell.urlString isEqualToString:nil]){
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"www.google.com"]];
    }

}
4

1 に答える 1

2

このUITableViewDelegateメソッドを使用します。

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

そこからセルにアクセスできます[tableView cellForRowAtIndexPath:indexPath]

編集

また、文字列を。と比較しないでくださいyourString == @"someString"

を使用し[yourString isEqualToString:@"someString"]ます。

*編集2*

CustomCellにプロパティを追加します

@property (nonatomic) NSString *urlString;

セルを作成するときにプロパティを設定してから、didSelectRowAtIndexPathメソッドでこれを行います。

CustomCell *cell = (CustomCell*)[tableView cellForRowAtIndexPath:indexPath];

これで、URLは単純になりますcell.urlString

于 2012-10-19T23:19:27.967 に答える