作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用自定义单元格创建 tableView。这是单元格 .h 文件的代码
@interface ZUITableViewCell : UITableViewCell {
IBOutlet UITextField *_textFieldOutlet;
IBOutlet UILabel *_textLabelOutlet;
}
@property (retain, nonatomic) IBOutlet UITextField *_textFieldOutlet;
@property (retain, nonatomic) IBOutlet UILabel *_textLabelOutlet;
@end
在我的表格 View Controller 文件中我创建了这个
#import <UIKit/UIKit.h>
#import "ZUITableViewCell.h"
@protocol ZUITableViewControllerDelegate ///for main view controller
-(void) doneButtonPressed;
@end
@interface ZUITableViewController : UITableViewController {
id <ZUITableViewControllerDelegate> delegate;
NSMutableArray *menuItems;
BOOL isAddingFields;
}
@property (nonatomic, retain) NSArray *menuItems;
@property (nonatomic, assign) id<ZUITableViewControllerDelegate> delegate;
- (CGRect) GetCellHegiht: (ZUITableViewCell *) cell;
@end
当用户选择第二行时,我想在 TableView 中添加新行。为此,我描述了这个方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
NSLog (@"row = %d", indexPath.row);
NSLog(@"INIT %p", self);
ZUITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell._textFieldOutlet becomeFirstResponder];
if (indexPath.row == 1 && isAddingFields == FALSE) {
isAddingFields = TRUE;
[menuItems insertObject:@"CC" atIndex:1];
[menuItems insertObject:@"BCC" atIndex:2];
NSIndexPath *path1 = [NSIndexPath indexPathForRow:1 inSection:0];
NSIndexPath *path2 = [NSIndexPath indexPathForRow:2 inSection:0];
NSArray *indexArray = [NSArray arrayWithObjects:path1,path2,nil];
[tableView insertRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationTop];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[path1 release];
[path2 release];
[cell._textFieldOutlet becomeFirstResponder];
}
}
这是我的 cellForRowAtIndexPath 方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
NSLog(@"INIT %p", self);
NSLog (@"cell for row = %d", indexPath.row);
ZUITableViewCell *cell = (ZUITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelsObjects = [[NSBundle mainBundle] loadNibNamed:@"ZUITableViewCell" owner:nil options:nil];
for (id currentObject in topLevelsObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (ZUITableViewCell *) currentObject;
break;
}
}
}
if (indexPath.row == 3 && isAddingFields == FALSE){
// [cell setBounds:[self GetCellHegiht:cell]];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell._textLabelOutlet.text = [menuItems objectAtIndex:indexPath.row];
cell._textFieldOutlet.tag = indexPath.row;
[cell._textFieldOutlet setEnabled:YES];
if(indexPath.row == 0){
[cell._textFieldOutlet becomeFirstResponder];
}
return cell;
}
在 Debug模式下,我可以在单击indexPath.row == 1的行后看到,该 Controller 使用indexPath.row = 1和2调用cellForRowAtIndexPath方法两次。但是After It Controller 调用方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
return [menuItems count];
}
再次调用 cellForRowAtIndexPath 方法,程序出现此错误后(我启用僵尸)错误 -[NSIndexPath release]:消息发送到已释放实例 0x6a9d790
最佳答案
方法[NSIndexPath indexPathForRow:inSection:]
(在path1和path2中使用)返回一个自动释放的对象,之后不需要调用release
。
关于objective-c - 错误-[NSIndexPath 发布] : message sent to deallocated instance 0x6a9d790,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8952406/
我是一名优秀的程序员,十分优秀!