I've read quite a few tutorials online about how to create a custom cell subclass, but I'm still a little confused. When I try to follow the instructions found in this question, I end up with errors that the tableView is not a valid property for the object of ViewController.
I've created a new subclass of UITableViewCell
, called CustomBookClass
. I've hooked up the properties with my CustomBookClass.h
file.
#import <UIKit/UIKit.h>
@interface CustomBookCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *bookImage;
@property (weak, nonatomic) IBOutlet UILabel *bookTitle;
@property (weak, nonatomic) IBOutlet UILabel *dateAdded;
@end
I then go into my ViewController.m
file to edit the viewDidLoad
method.
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView.delegate = self];
[self.tableView.dataSource=self];
[self.tableView registerClass:[CustomBookCell class]forCellReuseIdentifier:@"Custom
Cell"];
}
I get an error on the tableView, saying the property doesn't exist, even though in the ViewController.h
file, I'm including the table view.
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITableViewDelegate,
UITableViewDataSource>
@end
I'm sure I'm missing something really obvious here, as this is my first time trying this. Can anyone help me out? Thanks!
UIViewController
does not have that property. Even if you assign the Delegate and DataSource protocols to it. There are a couple of things you can do however.
ViewController
yourself. That means, create a property/outlet called tableView
. UITableViewController
instead of UIViewController
. Both should work.
Edit: Oh and the lines:
[self.tableView.delegate = self];
[self.tableView.dataSource=self];
don't make sense. They should be either:
self.tableView.delegate = self;
and self.tableView.dataSource = self;
[self.tableView setDelegate:self];
and [self.tableView setDataSource:self]
with 1 being preferred. (Edit: option #2 was actually incorrect code, my bad.)
No need to set delegate and datasource in TableViewCell class
simple make a property and synthesize your table view item
@property (weak, nonatomic) IBOutlet UIImageView *bookImage;
@property (weak, nonatomic) IBOutlet UILabel *bookTitle;
@property (weak, nonatomic) IBOutlet UILabel *dateAdded;
now import your cell class in your tableView controller class
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
//If you are using xib for representing your UI then add a nib file.
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.bookTitle.text = [tableData objectAtIndex:indexPath.row];
cell.bookImage.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
cell.dateAdded.text = [prepTime objectAtIndex:indexPath.row];
return cell;
}
for more detail check this link
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.