Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 从Nib加载UITableView_Iphone_Objective C_Uitableview_Crash - Fatal编程技术网

Iphone 从Nib加载UITableView

Iphone 从Nib加载UITableView,iphone,objective-c,uitableview,crash,Iphone,Objective C,Uitableview,Crash,加载UITableView时,我总是遇到崩溃。我正在尝试使用nib文件中定义的单元格 我在视图控制器头文件中定义了一个IBOutlet: UITableViewCell *jobCell; @property (nonatomic, assign) IBOutlet UITableViewCell *jobCell; 这在实现文件中进行了综合 我在IB中创建了一个UITableViewCell,并将其标识符设置为JobCell 以下是cellForRowAtIndexPath方法: - (UI

加载UITableView时,我总是遇到崩溃。我正在尝试使用nib文件中定义的单元格

我在视图控制器头文件中定义了一个IBOutlet:

UITableViewCell *jobCell;
@property (nonatomic, assign) IBOutlet UITableViewCell *jobCell;
这在实现文件中进行了综合

我在IB中创建了一个UITableViewCell,并将其标识符设置为JobCell

以下是cellForRowAtIndexPath方法:

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

static NSString *cellIdentifier = @"JobCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"JobsRootViewController" owner:self options:nil];
    cell = jobCell;
    self.jobCell = nil;
}

// Get this job
Job *job = [fetchedResultsController objectAtIndexPath:indexPath];

// Job title
UILabel *jobTitle;
jobTitle = (UILabel *)[cell viewWithTag:tagJobTitle];
jobTitle.text = job.title;

// Job due date
UILabel *dueDate;
dueDate = (UILabel *)[cell viewWithTag:tagJobDueDate];
dueDate.text = [self.dateFormatter stringFromDate:job.dueDate];

// Notes icon
UIImageView *notesImageView;
notesImageView = (UIImageView *)[cell viewWithTag:tagNotesImageView];
if ([job.notes length] > 0) {
    // This job has a note attached to it - show the notes icon
    notesImageView.hidden = NO;
}
else {
    // Hide the notes icon
    notesImageView.hidden = YES;
}

// Job completed button

// Return the cell
return cell;
}

当我运行应用程序时-我遇到一个硬崩溃,控制台报告如下:

objc[1291]:已释放(id):发送到已释放对象的消息样式=0x4046400

我已正确连接了IB中的所有插座。问题是什么


谢谢,

您的问题在这一块:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"JobsRootViewController" owner:self options:nil];
    cell = jobCell; <- THIS IS A SHALLOW COPY
    self.jobCell = nil; <- YOU JUST RELEASED IT
}

// Get this job
Job *job = [fetchedResultsController objectAtIndexPath:indexPath];

// Job title
UILabel *jobTitle;
jobTitle = (UILabel *)[cell viewWithTag:tagJobTitle]; <- CELL ISNT THERE ANYMORE
UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
如果(单元格==nil){
[[NSBundle mainBundle]loadNibNamed:@“JobsRootViewController”所有者:自选项:nil];

cell=jobCell;最简单的方法是将其分解为两个独立的nib文件,并从单元格自己的nib加载单元格。需要注意的是,您不能这样设置插座,如果您想这样做,您将需要一个自定义的UITableViewCell子类。但如果您不在单元格中连接任何内容,您可以简单地执行以下操作:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
    cell = [[[NSBundle mainBundle] loadNibNamed:@"JobsRootViewCell" owner:nil options:nil] objectAtIndex:0];
}
Swift 3

  • 单元格
您应该注册xib文件,以便在tableView中使用它:

override func viewDidLoad() {
        super.viewDidLoad()
        tableView.registerNib(UINib(nibName: "CellXibName", bundle: nil), forCellReuseIdentifier: "CellReuseIdentifier")
    }
UITebleViewDataSource方法:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("CellReuseIdentifier", forIndexPath: indexPath) as! CellClass
    return cell
}
表格视图

if let tableView: CustomTableView = nib?.first as? CustomTableView 
{
   self.view.addSubview(tableView)
}

xcode中有一个很好的小复选框,您可以将其命名为NSZombiesEnabled。这将跟踪已释放的对象并告诉您它们是什么。启用它并重新运行代码,以确定向任何对象发送了什么消息。这可能有助于了解发生了什么。请查看上的文档并转到部分标签ed“查找内存泄漏”-这将向您展示如何操作。我对Xcode IDE非常陌生。我不知道如何启用此设置…”要在应用程序中激活NSZombieEnabled工具,请选择“项目>编辑活动可执行文件”以打开可执行文件信息窗口。单击“参数”。单击对话框中的“添加(+)”按钮“要在环境中设置的变量”部分。在“名称”列中输入NSZombieEnabled,在“值”列中输入YES。确保选中了NSZombieEnabled项的复选标记编辑:这将帮助您准确地了解发生了什么。您正在发布一些不应该发布的内容,这导致了您的崩溃。感谢saramah到目前为止的帮助。启用僵尸后,我发现以下错误:**-[UITableView样式]:消息已发送到解除分配的实例0x4038600。我不确定调用[UITableView样式]的原因。我将UITableViewController按如下方式推送到导航堆栈上:JobsRootViewController*JobsRootViewController=[[JobsRootViewController alloc]initWithNibName:@“JobsRootViewController”捆绑包:nil];而且我从未设置TableViewStyle??我怀疑IB在设置UITableView时为您设置了对style的调用。您的代码中是否有任何UITableView实例?您是否在任何地方发布它们?如果没有,请检查您在xib中的连接。如果您仍然无法弄清楚,我建议取消xib并创建一个新实例,如下所示说明-尝试精确控制正在发生的事情,并检查每一步的正确性。注释:self.jobCell=nil;没有区别-我仍然会遇到相同的崩溃和错误。嗯,问题一定在代码中的其他地方。尝试在启用NSZombieEnabled的情况下确定您要向哪些对象发送消息。然后确定您的位置编码您正在发送的特定消息。然后找出您在哪里意外释放了您正在向其发送消息的对象。self.jobCell=nil;不会释放该单元格。它只会使jobCell指针指向nil,如果这是指向该单元格的最后一个指针,则会造成内存泄漏。
[self.jobCell release]
将释放它。