【发布时间】:2014-05-20 14:12:01
【问题描述】:
我遇到了一个让我头疼的问题。当我打电话给:
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath`
我总是得到indexPath=null 并出现以下错误。
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Categories copyWithZone:]: unrecognized selector sent to instance 0xc386b20'
我的GallerySpirituosenViewController.h 中有此代码:
@interface GallerySpirituosenViewController : UIViewController <iCarouselDataSource, iCarouselDelegate , UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *table_categories;
@property (weak, nonatomic) IBOutlet UITableView *table_brands;
@property (weak, nonatomic) IBOutlet UITableView *table_items;
@property (weak, nonatomic) IBOutlet iCarousel *carousel;
@property (weak, nonatomic) IBOutlet UIView *filtersContainer;
@end
稍后在我的 GallerySpirituosenViewController.m 中,我扩展了正常工作的“iCarrousel”,我有一个视图,其中包含 3 个 UITableView。
在这里我可以展示我如何为一张桌子调用datasource 和delegate:
- (void)viewDidLoad
{
[super viewDidLoad];
self.table_brands.dataSource = self;
self.table_brands.delegate = self;
_carousel.type = iCarouselTypeCoverFlow2;
}
- (void)viewDidUnload
{
[super viewDidUnload];
//free up memory by releasing subviews
self.carousel = nil;
}
然后是我填充表格的代码:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [categories count];//arrays
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// NSInteger carouselIndex = [self.carousel indexOfItemViewOrSubview:tableView];
// PersonData *data = [items objectAtIndex:carouselIndex];
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [categories objectAtIndex:indexPath.row];
return cell;
}
这里是对象类别的调试:
还有空值的indexPath:
这里也是对象 Category.h
#import <Foundation/Foundation.h>
@interface Categories : NSObject
@property (nonatomic, retain) NSString *id;
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *sys_language_uid;
@end
和 Category.m
#import "Category.h"
@implementation Categories
@synthesize id,title,sys_language_uid;
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:id forKey:@"id"];
[encoder encodeObject:title forKey:@"title"];
[encoder encodeObject:sys_language_uid forKey:@"sys_language_uid"];
}
- (id)initWithCoder:(NSCoder *)decoder {
if (self = [super init]) {
self.id = [decoder decodeObjectForKey:@"id"];
self.title = [decoder decodeObjectForKey:@"title"];
self.sys_language_uid = [decoder decodeObjectForKey:@"sys_language_uid"];
}
return self;
}
@end
【问题讨论】:
-
告诉我们如何创建类别变量并为其赋值?它是一个包含 NSString 的 NSMutableArray 吗?
-
复制这段代码并不容易,因为它来自项目的其他站点,而是一个 NSMutableArray,里面有对象。我用这段代码阅读了这个 MtableArray:-(NSMutableArray *) load: ( NSString * ) name { NSMutableArray * items = [NSKeyedUnarchiver unarchiveObjectWithFile:[utils pathByName:name]]; if ( items == nil ){ items = [NSMutableArray new]; } 退换货品; }
-
indexPath 不为空。 indexPath 实例中的索引属性为 NULL。在我的表格视图中这似乎不是问题,我认为您的问题不在这里。
-
嗯,感谢您的评论。能给个小费吗?
标签: ios objective-c uitableview icarousel