Iphone 设置UITableViewCell碰撞模拟器
在我的UIView nib文件中,我有一个UITableView,它占据了大约一半的屏幕。相应的.h和.m文件为:Iphone 设置UITableViewCell碰撞模拟器,iphone,uitableview,uiviewcontroller,nsarray,Iphone,Uitableview,Uiviewcontroller,Nsarray,在我的UIView nib文件中,我有一个UITableView,它占据了大约一半的屏幕。相应的.h和.m文件为: // helloViewController.h #import <UIKit/UIKit.h> @interface helloViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> { NSArray *locationArray; } @pro
// helloViewController.h
#import <UIKit/UIKit.h>
@interface helloViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
NSArray *locationArray;
}
@property (nonatomic, retain) NSArray *locationArray;
@end
// helloViewController.m
@synthesize locationArray;
- (void)viewDidLoad {
locationArray = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"11", nil];
[super viewDidLoad];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[[cell textLabel] setText: [locationArray objectAtIndex:[indexPath row]]];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 8;
}
。。。它不会崩溃
是什么导致了这个问题?委托和数据源连接到IB中的文件所有者,并且文件所有者的类设置为正确的类。我在nib的uiview中使用表视图是否是一个问题?这可能是因为以前没有保留locationArray,现在指向垃圾内存。或locationArray在指定索引处不包含项。问题是您正在将
locationArray
设置为viewDidLoad
中的自动释放对象。然后,尝试在要设置单元格的位置再次访问此对象,但此时数组已被解除分配
您应该使用您定义的retain属性(您直接设置数组,而不是使用该属性),并阅读有关内存管理的更多内容)
是的,这是因为数组中保留了一些内存。你应该在dealloc方法中释放位置数组Ivar
NSArray *array = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",nil];
self.locationArray = array ;
[array release];
在解除锁定方法中
[locationArray release];
我没有使用locationArray作为属性吗?我有@property ed和@synthesis-d它。我的印象是,当您这样做时,它会被保留…?您将其设置为
@属性
,但是当您设置它时,您只将其设置为ivar本身,而不是属性。因此,您应该将其设置为self.locationArry=…
哦,是的,我忘了提到这一点。感谢jbrennan的澄清,我将相应地编辑我的答案。
NSArray *array = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",nil];
self.locationArray = array ;
[array release];
[locationArray release];