gpt4 book ai didi

ios - 不知道我应该把逻辑放在 UITableViewController 还是 UITableViewCell

转载 作者:行者123 更新时间:2023-12-01 17:54:12 26 4
gpt4 key购买 nike

我有一个 UITableViewCell

  • 两个标签
  • 一个 TextView
  • 一个“接受”按钮
  • 一个“删除”按钮

  • 接受和删除按钮本质上都是删除单元格。接受更改某些数据的状态,它将显示在另一个屏幕上,删除将完全删除它。

    这是我显示 tableviewcell 的方法
    - (InvitationCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString* cellIdentifier = @"Identifier";
    InvitationCell *cell = (InvitationCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];


    // Create a new PFObject Object
    PFObject *connection = nil;

    connection = [self.tableData objectAtIndex:[indexPath row]];
    PFObject *inviterCodeName = connection[@"Inviter"];
    [inviterCodeName fetch];

    cell.inviterCodeName = inviterCodeName; // Allows us to use this in the cell
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
    [dateFormatter setLocale:enUSPOSIXLocale];
    [dateFormatter setDateFormat:@"EEE, MMM, h:mm a"];

    cell.dateLabel.text = [dateFormatter stringFromDate:[connection updatedAt]];


    cell.codeName.text = [NSString stringWithFormat:@"Invitee Code Name: %@", inviterCodeName[@"codeName"]];
    if (inviterCodeName[@"description"] == nil) {
    cell.codeNameDescription.text = @"";
    } else {
    cell.codeNameDescription.text = [NSString stringWithFormat:@"%@", inviterCodeName[@"description"]];
    }
    return cell;
    }

    我不确定是我是否将接受/拒绝方法放在实际的 UITableViewCell 类中,还是将它们保留在具有所有 UITableView 方法的类中?

    如果我将它们保留在具有所有 UITableView 方法的类中,如何使用自定义单元格中的两个按钮?我给他们做导出吗?我需要使用另一种委托(delegate)方法吗?

    最佳答案

    关于你的第一个问题,我想说以下。不要不是 将逻辑放入 UITableViewCell .单元格不是 Controller ,而是 View 。所以,它里面不应该有逻辑。 Controller 是正确的位置。

    可以在 UITableViewCell Is Not a Controller 中找到关于它的一个很好的讨论。 .我真的建议阅读它。

    关于第二次讨论,您可以遵循几种方法。例如,您可以直接在 Controller 中注册操作或使用委托(delegate)模式。我更喜欢后者,因为它对我来说更清楚。但这是个人口味。例如,它允许在另一个执行类似操作的 Controller 中重用这些单元。

    这是关于如何实现它的旧讨论。但它仍然有效。 Recipes to pass data from UITableViewCell to UITableViewController .

    希望能帮助到你。

    关于ios - 不知道我应该把逻辑放在 UITableViewController 还是 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21219479/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com