tableView reloadData не работает: cellForRowAtIndexPath не вызывается - PullRequest
1 голос
/ 23 сентября 2011

У меня есть эта проблема cellForRowAtIndexPath не вызывается reloadingData. Я провел много времени с этим. Я xib файл dataSource и delegate подключены к File's Owner. Любая идея приветствуется.

Это мой код:

* cellForRowAtIndexPath *

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

        //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        cell.backgroundView.opaque = NO;
        //cell.alpha = 0.65;

        cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.textLabel.opaque = NO;
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.highlightedTextColor = [UIColor whiteColor];
        cell.textLabel.font = [UIFont boldSystemFontOfSize:18];

        cell.detailTextLabel.backgroundColor = [UIColor clearColor];
        cell.detailTextLabel.opaque = NO;
        cell.detailTextLabel.textColor = [UIColor whiteColor];
        cell.detailTextLabel.highlightedTextColor = [UIColor whiteColor];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:14];

        NSString *temp = [distanceArray objectAtIndex:indexPath.row];
        if([checkMarkArray count] != 0){
          NSString *checkString = [checkMarkArray objectAtIndex:0];
          NSLog(@" checkString %@",checkString);
          if ([temp isEqualToString:checkString ]) {
            cell.accessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checked.png"]] autorelease];
        }
        else
        {
            cell.accessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"unchecked.png"]] autorelease];
        }
        }

    }

    // Set up the cell...
    [[cell textLabel] setText: [distanceArray objectAtIndex:indexPath.row]] ;
   return cell;
}

didSelectRowAtIndexPath ****

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
    cellText = selectedCell.textLabel.text;
    NSLog(@"%@",cellText);
    [checkMarkArray removeAllObjects];

    if([[tableViewDistance cellForRowAtIndexPath:indexPath] accessoryType] == UITableViewCellAccessoryCheckmark)
    {

        //global array to store selected object
        [checkMarkArray removeObject:[distanceArray objectAtIndex:indexPath.row]];
        NSLog(@"array %@",checkMarkArray);
        [tableViewDistance cellForRowAtIndexPath:indexPath].accessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"unchecked.png"]] autorelease];

        [[tableViewDistance cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];

          [self.tableViewDistance  reloadData];
    }
    else
    {
        [checkMarkArray addObject:[distanceArray objectAtIndex:indexPath.row]];

        NSLog(@"array again %@",checkMarkArray);

        [tableViewDistance cellForRowAtIndexPath:indexPath].accessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checked.png"]] autorelease];

        [[tableViewDistance cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
          [self.tableViewDistance  reloadData];

    }
    tableViewDistance.delegate = self;
    tableViewDistance.dataSource = self;
    [self.tableViewDistance  reloadData];
}

* viewDidLoad ***

- (void)viewDidLoad
{
    distanceArray= [[NSMutableArray alloc] initWithObjects:@"20 Kilomètres", @"40 Kilomètres", @"60 Kilomètres",@"80 Kilomètres",nil];
    NSLog(@"distance %@",distanceArray);
    tableViewDistance.backgroundColor=[UIColor clearColor];
    checkMarkArray = [[NSMutableArray alloc] init];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

Ответы [ 4 ]

2 голосов
/ 23 сентября 2011

Я нашел несколько слабых мест в вашем коде:

  1. Вы должны установить cell.accessoryView из if (cell == nil){}

  2. В - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath вы можете использовать tableView и не использовать tableViewDistance (может быть, это nil?)

  3. В - (void)viewDidLoad сначала следует позвонить [super viewDidLoad];, а затем выполнить настройки.

Надеюсь, это поможет вам. Gl

1 голос
/ 23 сентября 2011

подключите вашу розетку tableView (UITableView * tableViewDistance) к вашему Xib TableView.

0 голосов
/ 23 сентября 2011

Исходя из вашего комментария к вопросу, вы хотите, чтобы за раз выбиралась только одна ячейка, поэтому вы хотите отменить выбор ранее выбранной ячейки и выбрать ту, которая выбрана в настоящий момент. Вы можете использовать -indexPathForSelectedRow метод UITableView, чтобы найти выбранную строку:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  // For multiselect, use an array to store the checked state instead of using the tableView's state.
  [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow]];
  [self.tableView cellForRowAtIndexPath:indexPath].accessoryView = ...;
}

Вы также захотите реализовать tableView:didDeselectRowAtIndexPath::

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
  [self.tableView cellForRowAtIndexPath:indexPath].accessoryView = ...;
}
0 голосов
/ 23 сентября 2011

Попробуйте позвонить

[tableView reloadData];

вместо

 [self.tableViewDistance  reloadData];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...