滚动关闭然后重新打开后,表视图单元格只能正确遮罩 [英] Table view cell only masks correctly after scrolling off and then back on

查看:121
本文介绍了滚动关闭然后重新打开后,表视图单元格只能正确遮罩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在调用 tableView:cellForIndexPath 期间修改了一个表视图单元格,但是在单元格滚动然后重新打开之前,修改不会出现。我意识到我可以修改.xib文件或者可以分配/初始化一个单元而不是调用 dequeue ,但我想了解发生了什么。

I'm modifying a table view cell during a call to tableView:cellForIndexPath, but the modifications do not appear until the cell is scrolled off and then back on. I realize that I can modify the .xib file or can alloc/init a cell rather than calling dequeue, but I'd like to understand what is going on.

这些变化涉及屏蔽单元格以实现圆角底角。如果我设置角半径(即 cell.layer.cornerRadius = ... ),这会使所有角落都圆,我看不出问题。

The changes involve masking the cell to achieve rounded bottom corners. If I set the corner radius instead (i.e. cell.layer.cornerRadius = ...), which rounds all the corners, I don't see the problem.

更新

我最初没有发布代码,因为它是专有的,涉及很多复杂性,我认为这个问题在没有任何细节的情况下是合理的。然而,根据响应,这里是一个显示问题的简化片段,唯一的变化是实际的小区标识符。我还更新了描述/问题以简化方案。

I didn't post the code initially because it was proprietary, involved a lot of complexity and I thought the question was a reasonable one in absence of any specifics. In light of the responses, though, here is a reduced fragment which exhibits the problem, with the only change being the actual cell identifier. I also updated the description/question to simplify the scenario.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TheCorrectCellIdentifier" forIndexPath:indexPath];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:cell.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight                                                         cornerRadii:CGSizeMake(4., 4.)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = cell.bounds;
    maskLayer.path = maskPath.CGPath;
    cell.layer.masksToBounds = YES;
    cell.layer.mask = maskLayer;
    return cell;
}


推荐答案

您的代码正常运行。但是如果您仍然在 cellForRowAtIndexPath:中遇到Corner radius问题,那么请尝试 willDisplayCell:。我尝试了两种方法并且工作正常。

Your code working fine . But if you still facing Corner radius issue in cellForRowAtIndexPath: then try in willDisplayCell: . I tried in both methods and working fine.

willDisplayCell:


告诉委托表视图是否要为
特定行绘制一个单元格。


表视图将此消息发送给它在使用单元格绘制一行之前委托
,从而允许委托给
在显示之前自定义单元格对象。此方法为
委托提供了一个机会,可以通过表视图覆盖先前设置为
的基于状态的属性,例如选择和背景颜色。
委托返回后,表视图仅设置alpha和frame
属性,然后仅在向行或向外滑动时为行设置动画。
这是我的代码。

Tells the delegate the table view is about to draw a cell for a particular row.

A table view sends this message to its delegate just before it uses cell to draw a row, thereby permitting the delegate to customize the cell object before it is displayed. This method gives the delegate a chance to override state-based properties set earlier by the table view, such as selection and background color. After the delegate returns, the table view sets only the alpha and frame properties, and then only when animating rows as they slide in or out. Here is my code.

根据我的说法,Apple提供了两套不同的方法(协议),名为 UITableViewDataSource UITableViewDelegate 来管理UITableView。

According to me, Apple provide two different set of methods (Protocol) named UITableViewDataSource and UITableViewDelegate to manage UITableView.

UITableViewDataSource :数据源提供单元格的实际数据以填写详细信息。

UITableViewDataSource : Data source provides actual data for cell to fill in the details.

UITableViewDelegate :在另一端,委托负责提供有关tableview的可视布局的信息并处理用户交互。

UITableViewDelegate : At the other end delegate is responsible for give information about visual layout of tableview and handles user interaction.

因此,结论是DataSource主要处理表的内容(Data)和Delegate处理tableview组件的表示和交互。

So, conclusion is that DataSource mainly deal with content(Data) of table and Delegate deal with presentation and interaction of tableview component.

让我们来看看我们处理TableView。

Lets come to way we dealing with TableView.

我们都非常个人使用UITableViewDataSource方法 tableView:cellForRowAtIndexPath:,此方法负责创建或重用有效的UITableViewCell,并为每个索引路径提供适当的详细信息。我们倾向于在此方法中进行单元布局配置和UI修改。

We all are very personal with UITableViewDataSource method tableView:cellForRowAtIndexPath:, This method is responsible for to create or reuse valid UITableViewCell with proper detailed information for each index path.What we tend to do cell layout configuration and UI modification in this method.

调用 tableView:cellForRowAtIndexPath:后,系统会进行布局配置,然后调用 tableView:willDisplayCell:forRowAtIndexPath:在单元格显示在屏幕上之前的方法。

After tableView:cellForRowAtIndexPath: is called, the system does a layout configuration, then calls tableView:willDisplayCell:forRowAtIndexPath: method before cells display on screen.

因此我们可以使用此方法进行Tableview单元格的视图配置(可能是,Apple为我们提出了这种方法!!!)。

So we can use this method for view configuration of Tableview cell(May be, Apple propose this method for us!!!).

所以我经常更喜欢使用 tableView:cellForRowAtIndexPath: tableView创建单元格和左单元格UI配置任务:willDisplayCell: forRowAtIndexPath: method。

So I persionally prefer to use tableView:cellForRowAtIndexPath: to create cell and left cell UI configuration task for tableView:willDisplayCell:forRowAtIndexPath: method.

中应用掩码willDisplayCell:

 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:cell.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight                                                         cornerRadii:CGSizeMake(10.0, 10.0)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = cell.bounds;
    maskLayer.path = maskPath.CGPath;
    cell.layer.masksToBounds = YES;
    cell.layer.mask = maskLayer;
    }

这是我的 cellForRowAtIndexPath:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *identifier = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];

cell.textLabel.text = [NSString stringWithFormat:@"Row : %ld ", (long)indexPath.section];
return cell;
}

输出:

这篇关于滚动关闭然后重新打开后,表视图单元格只能正确遮罩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆