简体   繁体   English

离开屏幕后,如何将消息发送到当前选定的表格单元格?

[英]How can I send a message to the currently selected table cell after it has moved off-screen?

Here's my scenario: 这是我的情况:

I'm showing a UITableViewController in a UINavigationController , and I'm drawing the cells myself in a subclass. 我正在UINavigationController显示UITableViewController ,并在子类中自己绘制单元格。 In order to keep the cells looking as close to possible like native cells, I have a flag that indicates whether it is in a transitional state or not, in order to prevent the text color from visibly flashing when the user moves back up the stack from a detail view to the table view. 为了使单元格看起来像本机单元格一样近,我有一个标志来指示它是否处于过渡状态,以防止当用户从堆栈上移回堆栈时,文本颜色明显地闪烁。细节视图到表格视图。

Currently, I set my transitioning flag in -tableView:didSelectRowAtIndexPath: , like so: 目前,我在-tableView:didSelectRowAtIndexPath:设置了转换标志,如下所示:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // (stuff for pushing the detail view on to the navigation stack)

    ((MyCustomTableViewCell *) [self.tableView cellForRowAtIndexPath: indexPath]).transitioning = YES;
}

This works rather well, with one caveat: Immediately before the list animates off-screen, the transition is clearly visible to anyone looking for it, as the cell text changes to black (on blue) from white (on blue.) 需要注意的是,这种方法效果很好:列表动画在屏幕外之前,任何寻找它的人都可以清楚看到过渡,因为单元格文本从白色(蓝色)变为黑色(蓝色)。

My question: Is there any way to get the currently selected cell from the table view, after it has transitioned off-screen, and send it a message? 我的问题: 移出屏幕 ,有什么方法可以从表格视图中获取当前选定的单元格并向其发送消息吗? (assuming it isn't being deallocated, simply unloaded) (假设它没有被释放,只是被卸载)

Or am I simply going about this whole thing the wrong way? 还是我只是以错误的方式处理整个事情?

(For anyone considering saying that nobody will notice it, keep in mind that it's acceptable to me the way that it is, I'm simply wondering if there's a way for me to make it better. Good iOS applications are all about the little things.) (对于任何想说没人会注意到它的人,请记住,它对我来说是可以接受的,我只是想知道是否有一种方法可以使它变得更好。好的iOS应用程序只涉及很小的事情)

What do you mean by "prevent the text color from visibly flashing"? “防止文字颜色明显闪烁”是什么意思? By default iOS table cells don't appear to do that, at least in an unpleasant way. 默认情况下,iOS表格单元格似乎不执行此操作,至少以一种不愉快的方式。 Perhaps you can revisit your UITableViewCell implementation and determine if you are incorrectly handling -setSelected:animated: and -setHighlighted:animated 也许您可以重新访问UITableViewCell实现并确定是否错误地处理了-setSelected:animated:和-setHighlighted:animated

UITableView does not keep a publicly-accessible list of all the cells in the table. UITableView不会保留表中所有单元的可公开访问列表。
In order to access all the cells (including ones out of the screen) you need to maintain a separate array of the cells you generate. 为了访问所有单元(包括屏幕外的单元),您需要维护所生成单元的单独阵列。



@interface MyViewController : UIViewController 
{
   NSMutableArray* tableCells;
}



@implementation MyViewController

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableCells == nil)
      tableCells = [[NSMutableArray alloc] init];

    UITableViewCell* cell;
    if (indexPath.row < [tableCells count])
    {
        // Return a cell from the cached list
        cell = (UITableViewCell*)[tableCells objectAtIndex:indexPath.row];
    }
    else
    {
        // Create a new cell
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:@"identifier"];  

        // Customize and fill the cell with content anyway you wish
        // ...
    }

    return cell;
}

Now that you have a list of all the cells in the table, you can send them any message, anytime you want. 现在,您已经有了表中所有单元格的列表,可以随时向他们发送任何消息。

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

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