0

私は次のObjective-c関数を持っています

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


NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];

NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [mysearchdata objectForKey:key];

static NSString *SectionsTableID = @"SectionsTableID";
static NSString *TobyCellID = @"TobyCellID";

NSString *aName = [nameSection objectAtIndex:row];  

if (aName == @"Toby")
{
    TobyCell *cell = [tableView dequeueReusableCellWithIdentifier:TobyCellID];      
    if (cell == nil)
    {       
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TobyCell" owner:self  options:nil];
        for (id oneObject in nib)
            if ([oneObject isKindOfClass:[TobyCell class]])
                cell = (TobyCell *)oneObject;   
    }                                       
    cell.lblName.text = [nameSection objectAtIndex:row];
    return cell;        
}
else
{
 //standard cell loading code
}
}

私が欲しいのは、行が私の名前と等しいときにifステートメントを起動することだけです-非常にエキサイティングです。

if (aName == @"Toby")

アラートを入力しました。値が設定され、Tobyに設定されていますが、Ifステートメントがelse部分だけを実行していません。私が見逃しているのは明らかに単純なことです。

私はObjective-Cを学んでいます

4

1 に答える 1

9

このifステートメント:

if (aName == @"Toby")

文字列ではなく、ポインタを比較します。あなたが欲しい:

if ([aName isEqualToString:@"Toby"])

これはプレーンCと実際には違いはありません。==そこの文字列を比較するために使用することもできません。

于 2009-10-30T18:14:27.450 に答える