我在AppDelegate类中定义了NSMutableArray:
NSMutableArray *devices;
我从didFinishLaunchingWIthOptions方法中的类填充数组:
Devices *devs = [[Devices alloc] init];
self.devices = [devs getDevices];
getDevices方法解析一个json字符串并创建一个设备对象,将其添加到数组中:
NSMutableArray *retDevices = [[NSMutableArray alloc] initWithCapacity:[jsonDevices count]];
for (NSDiectionary *s in jsonDevices) {
Device *newDevice = [[Device alloc] init];
device.deviceName = [s objectForKey:@"name"];
[retDevices addObject: newDevice];
}
return retDevices;
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
Device *d = (Device *)[appDelegate.devices objectAtIndex:indexPath.row];
cell.label.text = d.deviceName;
最初填充tableview时,它会工作。但是,当我滚动列表时,cellForRowAtIndexPath再次执行时,d.deviceName会抛出一个错误,因为它超出了范围。
提前谢谢。