gpt4 book ai didi

ios - [cell addSubview :button] and [cell. contentview addsubview:button] 之间有什么区别

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:13:14 30 4
gpt4 key购买 nike

我在 tableviewcell 中有按钮。[cell addSubview:button][cell.contentview addsubview:button] 有什么区别?因为当我在 tableview 中使用 [cell addSubview:button] 时,按钮功能工作正常但我在 table view 中的界面搞砸了,在我 ScrollView 并返回 tableview 后按钮转到左侧单元格。另一方面,当我使用 [cell.contentview addsubview:button] 按钮功能不起作用并且没有任何内容保存到 myarray。

按钮代码:

UIButton *button=(UIButton *) [cell viewWithTag:103];//fav
[button setImage:[UIImage imageNamed:@"unfav.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"fav.png"] forState:UIControlStateSelected];
button.frame = CGRectMake(0,0, 50, 50);

button.tag = indexPath.row;
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button]; // add the button to the cell
[cell bringSubviewToFront:button];

最佳答案

contentView 是单元格的默认 View ,其中添加了所有 subviews。cell.view 是主视图,其中 contentView 本身被添加并且 cell.contentView 覆盖了 cell.view 的整个区域。但是在您将按钮添加到 cell.view 的情况下。

来自关于 ContentView 的苹果文档,

The content view of a UITableViewCell object is the default SuperView for content displayed by the cell. If you want to customize cells by simply adding additional views, you should add them to the content view so they will be positioned appropriately as the cell transitions into and out of editing mode.

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewCell_Class/#//apple_ref/occ/instp/UITableViewCell/contentView

关于ios - [cell addSubview :button] and [cell. contentview addsubview:button] 之间有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34249018/

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