iCarousel + UITableView = cellForRowAtIndexPath indexPath始终为null

时间:2014-05-20 14:12:02

标签: ios objective-c uitableview icarousel

我有一个让我头痛的问题。当我打电话给:

(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

在这里,我可以展示如何为一个表调用datasourcedelegate

- (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;
}

这是对象类别的调试:

enter image description here

带有null值的indexPath:

enter image description here

此处还有对象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

3 个答案:

答案 0 :(得分:1)

[categories objectAtIndex:indexPath.row]返回Categories对象。

你得到这样的字符串。

cell.textLabel.text = [categories objectAtIndex:indexPath.row].id

OR

cell.textLabel.text = [categories objectAtIndex:indexPath.row].title

修改更多内容:

您应该像这样声明title variable

@property(nonatomic,strong) NSString*title;

不是

@property(nonatomic,copy) NSString*title;

答案 1 :(得分:0)

你说:

  

当我致电:

  (UITableViewCell *)tableView:(UITableView *)tableView
  cellForRowAtIndexPath:(NSIndexPath *)indexPath` 
     

我一直都是   indexPath = null,出现以下错误。

方法tableView:cellForRowAtIndexPath:是一个tableview数据源方法。 你不应该自己调用这种方法。表视图在您的代码上调用该方法。

答案 2 :(得分:0)

据我所知,类别是一个对象,而不是一个字符串。这条线的目标是什么?

cell.textLabel.text = [categories objectAtIndex:indexPath.row];