【问题标题】:Changing UITableViewCell when selected选择时更改 UITableViewCell
【发布时间】:2013-02-24 18:37:57
【问题描述】:

在我的应用程序中,我有一个 UITableView,我有两个不同的自定义 UITableViewCells。 UITableView 最初加载了一种自定义 UITableViewCell。在我的 UITableView 中选择一个单元格时,我想更改使用另一种类型的自定义 UITableViewCell 选择的单元格。这可能吗?让我听听你得到了什么。

谢谢, NSNolan

#pragma mark - UITableView delegate methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.array count];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{    
    if(indexPath.row == [self currentCellIndex]) {
        [self setCurrentCellIndex:-1];

        NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"NewCell" owner:self options:nil];

        OldCell *cell = (OldCell *)[tableView cellForRowAtIndexPath:indexPath];
        cell = [nibs objectAtIndex:0];
    }
    else {
        [self setCurrentCellIndex:indexPath.row];

        NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"NewCell" owner:self options:nil];

        NewCell *cell = (NewCell *)[tableView cellForRowAtIndexPath:indexPath];
        cell = [nibs objectAtIndex:0];
    }        
}

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

    OldCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"OldCell" owner:self options:nil];
        cell = [nibs objectAtIndex:0];
    }

    return cell;
}

【问题讨论】:

  • 是否有特定的代码块给您带来麻烦? developer.apple.com/library/ios/#documentation/UserExperience/…
  • 我不了解您的要求或链接的相关性。
  • 您可以通过拥有一个保存当前选定 indexPath 的实例变量来实现此目的。然后重新加载表格视图。在 cellForRowAtIndexPath: 中,您可以检查 indexPath 是否等于 selectedIndexPath,然后初始化第二个自定义单元格。
  • 这正是我想要完成的,但我无法将新笔尖加载到旧单元格中。您有任何可以发布的代码示例吗?
  • @NSNolan 链接的相关性是表格视图编程指南。这是一个非常开放的问题。您应该尝试过什么,并指出您遇到的一个特定问题以及相关代码。请看这个:stackoverflow.com/questions/how-to-ask

标签: ios uitableview


【解决方案1】:

在您的didSelectRow... 方法中,您需要更新一个标志(存储在实例变量中),指示单元格的新“模式”。然后通过调用 tableView reloadRowsAtIndexPaths:withRowAnimation: 并传入选定的 indexPath 来重新加载行。

在您的 cellForRow... 方法中,使用标志来确定将两种类型的单元格中的哪一种用于行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 2012-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多