- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 UITableViewController
来显示来自 Web 服务的文章列表。检索到数据后,将调用此委托(delegate)方法:
-(void)itemsDownloaded:(NSArray *)items
{
// Set the items to the array
_feedItems = items;
// Reload the table view
[self.tableView reloadData];
}
我还使用自定义单元格,以便标签的高度发生变化,因此使用以下代码显示整个文章的标题(遵循本教程 Table View Cells With Varying Row Heights):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = @"BasicCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
[self configureCell:cell forRowAtIndexPath:indexPath];
return cell;
}
- (void)configureCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell isKindOfClass:[CustomTableViewCell class]])
{
CustomTableViewCell *textCell = (CustomTableViewCell *)cell;
Article *article_item = _feedItems[indexPath.row];
NSString *fulltitle = article_item.Title;
// fulltitle = article_item.Cat_Name; // testing category name
if (article_item.Subtitle != nil && article_item.Subtitle.length != 0) {
fulltitle = [fulltitle stringByAppendingString:@": "];
fulltitle = [fulltitle stringByAppendingString:article_item.Subtitle];
}
textCell.lineLabel.text = fulltitle;
textCell.lineLabel.numberOfLines = 0;
textCell.lineLabel.font = [UIFont fontWithName:@"Novecento wide" size:12.0f];
}
}
- (CustomTableViewCell *)prototypeCell
{
NSString *cellIdentifier = @"BasicCell";
if (!_prototypeCell)
{
_prototypeCell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
}
return _prototypeCell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
[self configureCell:self.prototypeCell forRowAtIndexPath:indexPath];
self.prototypeCell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.tableView.bounds), CGRectGetHeight(self.prototypeCell.bounds));
[self.prototypeCell layoutIfNeeded];
CGSize size = [self.prototypeCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return size.height+1;
}
第一个问题是 forRowAtIndexPath
方法被调用了两次而不是一次。因此,如果 _feeditems
有 10 个对象,该方法将被调用 20 次。第二次调用该方法时,我得到了 Article
对象 null
的两个属性(ID
和 Cat_Name
) > 自解除分配以来:
*** -[CFString retain]: message sent to deallocated instance 0x9c8eea0
*** -[CFNumber respondsToSelector:]: message sent to deallocated instance 0x9c8e370
这会在尝试显示类别名称时触发 EXC_BAD_ACCESS
。
我不确定到底是什么问题,我尝试删除代码以改变标签的高度,使用以下代码查看是否是导致此问题的原因:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Retrieve cell
NSString *cellIdentifier = @"BasicCell";
UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// Get article
Article *item = _feedItems[indexPath.row];
myCell.textLabel.text = item.Title;
return myCell;
}
唯一的区别是,如果 _feeditems
有 10 个对象,则该方法被调用一次意味着调用 10 次。但是 Article 的属性 ID
和 Cat_Name
仍在被释放。
在获取数据时,_feeditems
中所有对象的属性都完好无损,没有任何释放。我猜它发生在 cellForRowAtIndexPath
或 forRowAtIndexPath
中。
更新
正如@Ilya K 所建议的那样,不从 tableView:heightForRowAtIndexPath
调用 configureCell:forRowAtIndexPath:
可以解决调用两次的问题。我也试过拥有 feedItems
的属性。到目前为止,这是在 Controller
(TableViewController.m) 的 @interface
中设置的:
@interface TableViewController () {
HomeModel *_homeModel;
NSArray *_feedItems;
Article *_selectedArticle;
}
我已将其从界面中删除并将其添加为属性
(TableViewController.h):
@interface TableViewController : UITableViewController <HomeModelProtocol>
@property (weak, nonatomic) IBOutlet UIBarButtonItem *sidebarButton;
@property (nonatomic, strong) CustomTableViewCell *prototypeCell;
@property(nonatomic) NSString *type;
@property(nonatomic) NSString *data;
@property(copy) NSArray *_feedItems;
@end
尽管如此,它仍在提供已解除分配的消息。
更新 2
我已经使用 Instruments
和 Zombie
模板查看了代码(感谢这个问题的回答 ViewController respondsToSelector: message sent to deallocated instance (CRASH) )。这是我从 Instruments
得到的错误:
Zombie Messaged
An Objective-C message was sent to a deallocated 'CFString (immutable)' object (zombie) at address: 0x10c64def0
所有 Release/Retain Event Types
都指向以下方法,connectionDidFinishLoading
,当从 Web 服务检索 JSON 数据并创建 时会使用该方法检索到的每篇文章的文章
对象:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// Create an array to store the articles
NSMutableArray *_articles = [[NSMutableArray alloc] init];
// Parse the JSON that came in
NSError *error;
// Highlighted in blue
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:_downloadedData options:kNilOptions error:&error];
NSArray *fetchedArr = [json objectForKey:@"result"];
// Loop through Json objects, create question objects and add them to our questions array
for (int i = 0; i < fetchedArr.count; i++)
{
NSDictionary *jsonElement = fetchedArr[i];
// Create a new location object and set its props to JsonElement properties
Article *newArticle = [[Article alloc] init];
newArticle.ID = jsonElement[@"ID"];
newArticle.Title = jsonElement[@"Title"];
newArticle.Subtitle = jsonElement[@"Subtitle"];
newArticle.Content = jsonElement[@"Content"];
newArticle.ImageUrl = jsonElement[@"ImageUrl"];
newArticle.Author = jsonElement[@"Author"];
newArticle.PostId = jsonElement[@"PostId"];
newArticle.ArticleOrder = jsonElement[@"ArticleOrder"];
newArticle.Cat_Id = jsonElement[@"CategoryId"];
// Highlighted in yellow
newArticle.Cat_Name = jsonElement[@"CategoryName"];
// Add this article object to the articles array
// Highlighted in yellow
[_articles addObject:newArticle];
}
// Ready to notify delegate that data is ready and pass back items
if (self.delegate)
{
[self.delegate itemsDownloaded:_articles];
}
}
但我仍然无法弄清楚哪里出了问题。
更新 3
关于connectionDidFinishLoading
的更多测试我已经删除了正在解除分配的两个属性,并且未显示解除分配的消息。我不知道是什么原因导致这两个属性(ID
和 Cat_Name
)被释放,此时无法从任何地方访问它们。
最佳答案
您不需要调用 configureCell:forRowAtIndexPath: 从 tableView:heightForRowAtIndexPath: 您应该使用带有 sizeWithAttributes 的 Article 对象来确定单元格高度:
您的 prototypeCell 函数只是创建类型为 CustomTableViewCell 的不相关的空单元格,没有必要尝试重新调整它的大小。
tableView:cellForRowAtIndexPath:每次您的 tableview 需要重绘时调用,例如当您滚动时。这意味着你的 _feeditems 数组应该被分配并保持一致,以便在实例生命周期的任何时间点与 UITableView 一起工作。
还要确保为 _feeditems 声明一个属性并使用该属性分配数据。
例子:@property (strong) NSArray *feeditems;或@property(复制)NSArray *feeditems;
在已下载的项目中:self.feeditems = items;
关于ios - UITableViewController : Objects properties deallocated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24099013/
您好,下面是我在 AWS 中创建启用了弹性 IP 的实例的模板。但是我不确定我哪里出错了。我已经通过在线 json 验证器验证了 json,仍然面临问题请帮忙 { "AWSTemplateForm
标题有点乱,但已经说明了一切。我想在一个属性的属性上有一个属性观察器: class A { var b: B init() { b = B() } fu
假设我的一个 Controller 中有这样的方法: [Route("api/Products")] public IQueryable GetProducts() { return db.P
这有效: // @flow import React, {Component} from 'react'; type Props = {}; class Delete extends Componen
我有一个 ViewModelBase 类,我在其中为 INotifyPropertyChanged 接口(interface)定义了 RaisePropertyChanged 方法。大多数 MVVM
我创建了类: class StorageBase { public Queue Slices {get;set;} } 和 wpf 自定义控件,它具有 StorageBase 类型的依赖属性
我的 java 应用程序问题是 log4j2 系统日志不是写在 'local1.log' 中而是'消息'。我的/etc/rsyslog.conf 在/etc/rsyslog.conf 中配置为 'lo
为什么需要在对象中使用 this.property = property ?它是用来定义对象的“外部世界”的属性吗? function Person(property) { this.property
摘要: 这个问题是关于属性的继承与从彼此继承属性的类的内部和外部的不同读/写访问相结合。 详细信息: 我有一个类 A 和另一个继承自 A 的类 B。 A 中声明了属性someProperty。我希望该
我正在开发 ASP.NET MVC 应用,设计域模型,使用(测试)新的 EF Code First 功能。 我有一个可能有也可能没有截止日期的事件实体,处理它的最佳方法是什么? 1 个属性: publ
我在配置项目时经常使用它们,但大多数情况下都是按照指示添加 fragment 。我完全不知道哪个文件到底是做什么的。谁能清楚地说明每个文件的用途。 到目前为止我认为 local.properties
在运行 python 文件以使用 rasa nlu 训练文件时,我在命令提示符下收到此错误 我目前正在使用 Windows 10 rasa_core==0.8.2 rasa_nlu==0.11.4 p
我在这方面遇到了一些麻烦,尽管我已经搜索了答案,但还是找不到。 为了使用 AsyncAppender,我看到了很多不同的 log4j 配置,无论如何,它们都与 .properties 配置文件无关。
我正在编写一个 Python 类,并使用 @property 装饰器为该类创建属性。 我在文档中没有找到太多关于这个装饰器的信息,但是从我可以从 Stack Overflow 和我的 Python l
在 gradle 任务中,我可以创建这样的路径: System.env.FOLDER_PATH + '/subFolder' 但我想在我的 gradle.properties 中设置它,所以它会像 s
如何在属性文件的 log4j2 中创建键值对? 我知道在 log4j 版本 1 中它是这样完成的: log4j.appender.x.additionalFields={'key': 'value'}
我想通了 struct PropertyTest { @property int x() { return val; } @property void x( int newVal )
我有 REST (Jersey) webservice,它利用了一些编码/解码到/来自 XML 的数据对象。数据对象位于 webservice war 所依赖的单独项目/jar 中。 我使用 MOXy
我正在创建一个 LinkedList 类: function LinkedList(){ ... 有什么区别: this.addNode = function(data){
关于语义的快速问题:) 如果我正在编写一个协议(protocol),这是首选: // (a) @protocol MyProtocol @property (nonatomic, copy) NSSe
我是一名优秀的程序员,十分优秀!