我正在使用 Xcode 6,我创建了一个包含 UITableView
和 custom Cell
的应用程序。
这是我的自定义单元格
@interface SuggestingTableViewCell : UITableViewCell
@property (nonatomic, weak) IBOutlet SuggestedSeriesView *seriesOne;
@property (nonatomic, weak) IBOutlet SuggestedSeriesView *seriesTwo;
@property (nonatomic, weak) IBOutlet SuggestedSeriesView *seriesThree;
@property (nonatomic, weak) IBOutlet SuggestedSeriesView *seriesFour;
@end
如您所见,我有四个 IBOutets
到 SuggestedSeriesView
,它是 UIView
的子类。
在 TableView DataSource
方法中,我创建了这些 SuggestedSeriesView
并将它们分配为:
cellIdentifier = suggestionCell;
SuggestingTableViewCell *suggesting = (SuggestingTableViewCell *)[tableView dequeueReusableCellWithIdentifier:suggestionCell];
Series *ser1 = series[0];
suggesting.seriesOne = [[SuggestedSeriesView alloc] initWithFrame:suggesting.seriesOne.bounds
andSeriesData:@{JV_SERIES_IMAGE_URL : ser1.imageURL,
JV_SERIES_TITLE : ser1.title}];
Series *ser2 = series[1];
suggesting.seriesTwo = [[SuggestedSeriesView alloc] initWithFrame:suggesting.seriesTwo.bounds
andSeriesData:@{JV_SERIES_IMAGE_URL : ser2.imageURL,
JV_SERIES_TITLE : ser2.title}];
Series *ser3 = series[2];
suggesting.seriesThree = [[SuggestedSeriesView alloc] initWithFrame:suggesting.seriesThree.bounds
andSeriesData:@{JV_SERIES_IMAGE_URL : ser3.imageURL,
JV_SERIES_TITLE : ser3.title}];
Series *ser4 = series[3];
suggesting.seriesFour = [[SuggestedSeriesView alloc] initWithFrame:suggesting.seriesFour.bounds
andSeriesData:@{JV_SERIES_IMAGE_URL : ser4.imageURL,
JV_SERIES_TITLE : ser4.title}];
编译器给我的警告是:
Assigning retained object to weak property; object will be released after assignment
为什么 SuggestedSeriesView
会因为没有 IBOutlet
而被 cell
保留?
感谢您的帮助。
最佳答案
发生这种情况是因为你的属性很弱,这意味着它们不会保留任何东西,它们只能引用东西。
IBOutlet 等于 void,它只是提示 xcode 告诉它“这可以在界面构建器上连接”。
Interface Builder 中的属性类型为 weak 和 IBOutlet 的原因是,它们由 Storyboard 的 View Controller View 本身保留,因此如果您在 Interface Builder 中创建一个 View Controller,并添加一个 View ,然后在代码中链接此 View 您的属性不必很强,因为它已经被其中一个 View 保留。
您应该将这些属性更改为
@property (nonatomic, strong) SuggestedSeriesView *seriesOne;
@property (nonatomic, strong) SuggestedSeriesView *seriesTwo;
@property (nonatomic, strong) SuggestedSeriesView *seriesThree;
@property (nonatomic, strong) SuggestedSeriesView *seriesFour;
关于ios - 将保留的对象分配给弱属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26582530/