重绘UITableView

And*_*rew 1 iphone uitableview uiview ipad ios

我需要在代码中的事件上重写一个UITableView.但我尝试的一切似乎都不起作用.我试过了[self.tableView reloadData],我和代表们一起玩过.我试图实现的目标是使用不同格式的单元格呈现完全不同的UITableView.所以我需要重绘表格.任何帮助,将不胜感激.

继承我的代码:

       ...
    if/else...
    }
    //Now I want to reload the tableView
    [tableView reloadData];    //Isn't getting it done
    NSLog(@"index = %i", index);  //Always fires
    tableView.delegate = self;  // Didn't help
    [tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];  // Also nothing
}
Run Code Online (Sandbox Code Playgroud)

我想要做的是这样的:

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

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     if (cell == nil) {
         if (segmentOptions == snacks) {
             cell = [[TableViewCell1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
              NSLog(@"A");
         } else {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
            NSLog(@"B");
         }
     }
  ...
}
Run Code Online (Sandbox Code Playgroud)

Pau*_*l.s 5

你可能意味着什么

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = @"cell-not-snacks";

    if (segmentOptions == snacks) {
        cellIdentifier = @"cell-snacks";
    }

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
         if (segmentOptions == snacks) {
             cell = [[TableViewCell1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
         } else {
             cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
         }
     }
}
Run Code Online (Sandbox Code Playgroud)