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")

我已经发出警报,并且正在设置 Value 并将其设置为 Toby 但 If 语句不只是执行 else 部分。显然,我缺少一些简单的东西。

我正在学习 Objective-C

4

1 回答 1

9

if声明:

if (aName == @"Toby")

比较指针,而不是字符串。你要:

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

这与普通 C 并没有什么不同。您也不能==在那里比较字符串。

于 2009-10-30T18:14:27.450 回答