在下面的代码中,如果我们做 [cell addSubview: someLabel]vs [cell.contentView addSubview: someLabel],它们似乎工作相同.做一个或另一个有什么不同吗?(实际代码中的自定义单元格正在添加UIImageView和UILabel)(UIView另一方面,没有contentView,因此我们不需要将子视图添加到其中contentView. 顺便说一句UITableViewCell是子类UIView)
-(UITableViewCell *) tableView:(UITableView *) tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
if ([tableView isEqual:self.songsTableView]){
static NSString *TableViewCellIdentifier = @"MyCells";
cell = [tableView dequeueReusableCellWithIdentifier:TableViewCellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:TableViewCellIdentifier];
}
// ... some code to create a UILabel (not shown here)
[cell addSubview: someLabel]; // vs using [cell.contentView addSubView: ...]
Run Code Online (Sandbox Code Playgroud)
Zha*_*ang 11
我相信如果我没有错,contentView是UITableViewCell的子视图.
如果你在这里查看这个页面,你会发现UITableViewCell中实际上有3个子视图
我认为默认情况下,编辑控件是隐藏的,直到您进入表格的编辑模式,在这种情况下,编辑控件出现(每行左侧的减号按钮),您的contentView调整大小并推到右侧.这可能是另一个答案所提到的"适当的动画"效果.
要测试差异,请尝试将带有文本的UILabel等子视图添加到单元格而不是cell.contentView.当你将它添加到单元格而不是cell.contentView并进入你的表的编辑模式时,我相信你的UILabel不会调整大小,你会在减号按钮上方/下方看到编辑按钮.
| 归档时间: |
|
| 查看次数: |
4086 次 |
| 最近记录: |