Возникли проблемы с UITableViewCell

#iphone #objective-c #cocoa-touch #uitableview

#iPhone #objective-c #cocoa-touch #uitableview

Вопрос:

Я использую новый код стиля просмотра таблиц iOS5, но, похоже, у меня возникла проблема. Он выходит из строя всякий раз, когда вызывается функция searching = true . Я думаю, что достаточно просто передать объект UITableView в метод configure, который принимает BadgedCell. Есть идеи, как это исправить?

 Crash says: 2011-10-25 22:21:04.973 Social[5719:707] -[__NSCFDictionary isEqualToString:]: unrecognized selector sent to instance 0x392510

2011-10-25 22:21:04.975 Social[5719:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary isEqualToString:]: unrecognized selector sent to instance 0x392510'

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(searching)
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchCell"];
        [self configureCell:cell atIndexPath:indexPath];
        return cell;
    }
    else
    {
        TDBadgedCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BadgedCell"];
        [self configureCell:cell atIndexPath:indexPath];
        return cell;   
    }
}

- (void)configureCell:(TDBadgedCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    if(searching)
    {
        NSDictionary *dictionary = (NSDictionary *)[listOfItems objectAtIndex:indexPath.row];
        [cell.textLabel setText:[dictionary objectForKey:@"exerciseName"]];
        [cell.detailTextLabel setText:[dictionary objectForKey:@"muscleName"]];
        cell.textLabel.text = [listOfItems objectAtIndex:indexPath.row];
        cell.detailTextLabel.text = [listOfItems objectAtIndex:indexPath.row];
    }
    else
    {
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:16];
        cell.textLabel.text = [[self.muscleArray objectAtIndex:indexPath.row]objectForKey:@"muscleName"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
}
 

Ответ №1:

Вы передаете словарь listOfItems в виде строки в ячейку:

Если вы использовали это:

       NSDictionary *dictionary = (NSDictionary *)[listOfItems objectAtIndex:indexPath.row];
 

Я понимаю, что listOfItems содержит много словарей.
Поэтому, когда вы вызываете:

     cell.textLabel.text = [listOfItems objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = [listOfItems objectAtIndex:indexPath.row];
 

По сути, вы пытаетесь присвоить словарь метке ячейки. вот почему Xcode сообщает вам, что [__NSCFDictionary isEqualToString:] .

попробуйте удалить эти строки.

Ответ №2:

Вам нужно удалить эти две строки из вашего метода configure, потому что, если, как следует из предыдущих трех строк, ваш ‘listOfItems’ представляет собой список словарей, тогда вы пытаетесь установить словарь в качестве текстового поля textLabel, что вызовет всевозможные проблемы.

 cell.textLabel.text = [listOfItems objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [listOfItems objectAtIndex:indexPath.row];