【问题标题】:Updating tableview cells更新表格视图单元格
【发布时间】:2011-05-16 20:53:24
【问题描述】:

我仍然非常喜欢 Object-C,所以希望我能够很好地沟通这一点,所以希望我可以通过我的代码示例来解释这一点。

我有一张有四行的表格。每行都有一个标签(打卡时间、午餐时间等)和一个时间(detailTextLabel)。标签存储在一个数组中,细节是从 NSDate 系列函数生成的(请原谅我的术语)。为了更改时间值,我使用了一个调整为选择时间的数据选择器。使用选择器更改时间时,操作用于更新行的详细信息。

后面的代码 sn-ps 在同一个 *.m 类中。

这是一个剪裁的版本 -

// textLabel goes on the left, detailTextLabel goes on the right
// This puts the labelArray on the left and times on the right
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *kCustomCellID = @"CustomCellID";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCustomCellID];

    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kCustomCellID] autorelease];

        // This disable the hightlighter effect when we select a row.
        // We need the highlighter, but we'll leave the code here.
        // cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    cell.textLabel.text = [self.labelArray objectAtIndex:indexPath.row];


    // --- Start of routine to work with adding hours and minutes to stuff

    NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];

    // --- Set Clock In time (initially time 'now').  
    if (indexPath.row == [labelArray indexOfObjectIdenticalTo:@"Clock In"])
    {
        self.clockIn = [NSDate date];

        cell.detailTextLabel.text = [self.dateFormatter stringFromDate:clockIn];

        NSLog(@"Clock In - %@", clockIn);
        //NSLog(@"Clock In (cell*) - %@", cell.detailTextLabel.text);

        return cell;
    }    

    // --- Set Out to Lunch time (initially clockIn + 5 hours)
    if (indexPath.row == [labelArray indexOfObjectIdenticalTo:@"Out to Lunch"])
    {
        [offsetComponents setHour:5];

        self.outToLunch = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]
                              dateByAddingComponents:offsetComponents toDate:[NSDate date] options:0];

        cell.detailTextLabel.text = [self.dateFormatter stringFromDate:outToLunch];

        NSLog(@"indexPath.row (Out to Lunch): %i", indexPath.row);

        NSLog(@"Out to Lunch - %@", outToLunch);
        //NSLog(@"Out to Lunch (cell*) - %@", cell.detailTextLabel.text);

        return cell;
    }

    // Leaving out two other if clauses as they duplicate the Out to Lunch clause.

    //cell.detailTextLabel.text = [self.dateFormatter stringFromDate:[NSDate date]];
    //return cell;
    return nil;

这段代码运行良好,没有给我带来任何问题。

When a row, such as "Clock In" is selected an animiation is called that scroll's up a time picking datePicker.随着时间的滚动,“时钟”时间更新。

这就是我的问题所在。随着“打卡”时间的更新,我不知道如何更新“外出吃午饭”行。

这是我的拣货代码 -

- (IBAction)dateAction:(id)sender
{
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    cell.detailTextLabel.text = [self.dateFormatter stringFromDate:self.pickerView.date];

    NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];

    if (indexPath.row == [labelArray indexOfObjectIdenticalTo:@"Clock In"])
    {
        if (cell.detailTextLabel.text != [self.dateFormatter stringFromDate:clockIn])
        {
            NSLog(@"clockIn time changed...");

            // Since clockIn time changed, we need to change outToLunch, inFromLunch, and clockOut

            // Change the outToLunch time
            [offsetComponents setHour:5];

            self.outToLunch = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]
                               dateByAddingComponents:offsetComponents toDate:self.pickerView.date options:0];

            NSLog(@"New Out to Lunch time: %@", [self.dateFormatter stringFromDate:outToLunch]);

            // Here is where I get stuck
        }
        //return nil;
    }
}

我对“更改 outToLunch 时间”条款的设想是这样的......

  1. 将新的时钟输入时间加上 5 小时,并将其设置为新的 outToLunch 时间。 一种。 NSLog 只是为了查看这个数学函数是否有效。
  2. 使用 outToLunch 时间单元格的 detailTextLabel 的索引,将这个新时间放在那里。

将同时更新两行以及 outToLunch 行。

我该怎么做?

感谢您的帮助。

【问题讨论】:

    标签: iphone ios uitableview nsindexpath


    【解决方案1】:

    在您的 - (IBAction)dateAction:(id)sender 末尾使用以下内容:

        // Here is where I get stuck
          }
          //return nil;
      }
      NSArray *indexPathArray=[NSArray arrayWithObject:indexPath];
      [self.tableView reloadRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationNone];
    }
    

    或者,您可以使用 [self.tableView reloadData],但这对于仅重新加载单行来说太过分了,尤其是在您拥有的行数很大的情况下。

    【讨论】:

    • 谢谢,但我一定遗漏了一段代码,例如当我包含您的行时,应用程序在我单击选择器轮三次后崩溃。我已将其缩小到重新加载部分。这是简短的崩溃错误: *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“*** -[NSArray initWithObjects:count:]:尝试在对象 [0] 处插入 nil 对象”*** I我也在研究这个错误信息。
    • 在创建 indexPathArray 之前检查您的 indexPath 是否为 nil。很可能是在重新加载行时,该行被取消选中,导致 indexPath 为 nil 并导致 reloadRowsAtIndexPath 方法抛出异常。保持选中行可能不是最佳的 UI 实践。或者,您可以在 didSelectRowAtIndexPath 方法中设置一个实例变量,例如 mySelectedIndexPath。然后你可以使用 NSArray *indexPathArray=[NSArray arrayWithObject:self.mySelectedIndexPath];从上面的代码。
    • 您还应该释放您分配的一些对象。即 NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];运行静态分析器将帮助您识别这样的事情。
    • 感谢您帮助 timthetoolman 重新加载和释放/处理。我想知道什么工具可以帮助识别这些。
    【解决方案2】:

    在使用 timthetoolman 的有用建议和谷歌搜索的许多变体之后进行了这项工作。我发现这篇 stackoverflow 文章:“How to select the indexPath of the UITableView after a pop?Thomas 回答了我使用以下代码片段:

    添加到 .h 文件中

        NSIndexPath* outToLunchIndexPath;
    
            //...
        }
    
        //...
    
        @property (nonatomic, retain) NSIndexPath* outToLunchIndexPath;
    
        //...
    
        @end
    

    添加到 .m 文件中

    @synthesize outToLunchIndexPath;
    

    然后在我的代码中添加了这些行

    self.outToLunch = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]
                               dateByAddingComponents:offsetComponents toDate:self.pickerView.date options:0];
            // New code...
            UITableViewCell *outToLunchCell = [self.tableView cellForRowAtIndexPath:outToLunchIndex];
            outToLunchCell.detailTextLabel.text = [self.dateFormatter stringFromDate:outToLunch];
    
            NSLog(@"New Out to Lunch time: %@", [self.dateFormatter stringFromDate:outToLunch]);
    

    这具有更新原始空间的 cell.detailLabel.text 内容的影响,因为时钟时间随着 datePicker 的变化而变化。

    非常感谢 timthetoolmanThomas 的帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多