Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.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 设置UITableViewCell碰撞模拟器_Iphone_Uitableview_Uiviewcontroller_Nsarray - Fatal编程技术网

Iphone 设置UITableViewCell碰撞模拟器

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

在我的UIView nib文件中,我有一个UITableView,它占据了大约一半的屏幕。相应的.h和.m文件为:

// 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];