繁体   English   中英

重画一个UITableView

[英]Redraw a UITableView

我需要根据代码中的事件从字面上重绘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
}

我想要做的是这样的:

- (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");
         }
     }
  ...
}

你可能意思是

- (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];
         }
     }
}

我认为值得在UITableViewCell类中查看prepareForReuse方法。 在从UITableView方法dequeueReusableCellWithIdentifier:返回对象之前调用此方法:

您可以在自定义UITableViewCell上覆盖此方法(不要忘记包括[super prepareForReuse]),以便重绘您需要重绘的任何内容。

问题是: if (cell == nil) { 我删除了它,并且每次都能完美运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM