I'm trying to create my own custom cell, but for some reason, it isn't appearing. I read a lot of example, the code looks like (at least for me) equal to others. I used the interface builder to recreate it (I deleted the Default View, add a TableViewCell, put Identifier = CustomCell, and wire up the Label with valueLabel).
Can comeone help me?
Thanks in advance.
//CustomCell.h
#import <UIKit/UIKit.h>
#interface CustomCell : UITableViewCell {
IBOutlet UILabel *valueLabel;
}
#property(nonatomic, retain) IBOutlet UILabel *valueLabel;
#end
//CustomCell.C
#import "CustomCell.h"
#implementation CustomCell
#synthesize valueLabel;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)dealloc {
[super dealloc];
}
#end
//Where I'm trying to use this cell. Here I imported the .h file (#import "CustomCell.h"):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.valueLabel.text = #"Any text";
return cell;
}
Your cellForRowAtIndexPath method belongs in the table view controller? Is that where it is located?
Related
I have a TableViewCell subclass and I am trying to create a table with this cell type. However the table won't show the custom cells content.
The values of the cell derive from data core model. But there isn't an issue with it as I have also tried with simple literals and it still wouldn't work. Any help is appreciated.
Here is my code:
favCell.m
#import "favCell.h"
#implementation favCell
#synthesize view;
#synthesize namelbl;
#synthesize scorelbl;
#synthesize prodimage;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
view = [[UIView alloc] initWithFrame:self.frame];
[self addSubview:view];
// initiate
prodimage = [[UIImageView alloc]initWithFrame:CGRectMake(2,20, 20, 20)];
namelbl = [[UILabel alloc]initWithFrame:CGRectMake(3,20, 40, 40)];
scorelbl = [[UILabel alloc]initWithFrame:CGRectMake(70,20, 30, 30)];
[view addSubview:namelbl];
[view addSubview:scorelbl];
[view addSubview:prodimage];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
favTable.m
#import "favTable.h"
#import "ecoAppDelegate.h"
#import "favCell.h"
#interface favTable ()
#end
#implementation favTable
#synthesize favArr;
#synthesize managedObjectContext;
#synthesize fetchedResultsController;
#synthesize favName;
#synthesize favScore;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
favCell*cell = (favCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[favCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.namelbl.text = [favName objectAtIndex:indexPath.row];
cell.scorelbl.text = [favScore objectAtIndex:indexPath.row];
cell.prodimage.image = [UIImage imageNamed:#"test1.jpg"];
return cell;
}
I'm trying to populate the menu in ECSlidingViewController(a UITableVIEW) using the code below, but when I run the app the menu, or uitableview, is blank and not populated with the 3 MenuItems; Important, Invitation, and Agenda. I suspect the problem lies within didSelectRowAtIndexPath, but I'm not sure. Any help with this would be much appreciated. Thank you.
#import "MenuViewController.h"
#interface MenuViewController ()
#property (nonatomic, strong) NSArray *menuItems;
#end
#implementation MenuViewController
#synthesize menuItems;
- (void)awakeFromNib
{
self.menuItems = [NSArray arrayWithObjects:#"Important", #"Invitation", #"Agenda", nil];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.slidingViewController setAnchorRightRevealAmount:280.0f];
self.slidingViewController.underLeftWidthLayout = ECFullWidth;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
return self.menuItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"MenuItemCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [self.menuItems objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = [NSString stringWithFormat:#"%#", [self.menuItems objectAtIndex:indexPath.row]];
UIViewController *newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
[self.slidingViewController anchorTopViewOffScreenTo:ECRight animations:nil onComplete:^{
CGRect frame = self.slidingViewController.topViewController.view.frame;
self.slidingViewController.topViewController = newTopViewController;
self.slidingViewController.topViewController.view.frame = frame;
[self.slidingViewController resetTopView];
}];
}
1) Be sure to connect datasource and delegate outlets in IB, and
2) Is MenuViewController a subclass of UITableViewController? If not, then I think you're going to need to load the table data yourself someplace. Try this:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.tableView reloadData]; // need to have an outlet named this
}
If you don't have an outlet setup called tableView, pointing to the table view, create one in IB.
I suspect that your menuItems array is empty. Try populating it in viewDidLoad.
You may want to take a look at this for a discussion of awakeFromNib, viewDidLoad, initWithNibName.
#import "MasterViewController.h"
#import "DetailViewController.h"
#interface MasterViewController ()
#end
#implementation MasterViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Master", #"Master");
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
}
}
return self;
}
- (void)dealloc
{
[_detailViewController release];
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100.0;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil)
{
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.cellDate.text=#"date";
cell.cellDescription.text =#"Description";
cell.cellImageview.image = [UIImage imageNamed:#"facebook.png"];
cell.cellTitle.text = #"Title";
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
if (!self.detailViewController) {
self.detailViewController = [[[DetailViewController alloc] initWithNibName:#"DetailViewController_iPhone" bundle:nil] autorelease];
}
[self.navigationController pushViewController:self.detailViewController animated:YES];
}
else
{
}
}
#end
.h file
#import <UIKit/UIKit.h>
#import "CustomCell.h"
#import "XMLStringFile.h"
#class DetailViewController;
#interface MasterViewController : UITableViewController{
}
#property (strong, nonatomic) DetailViewController *detailViewController;
#property(strong,nonatomic)CustomCell *customCell;
#end
CustomCell.h
#import <UIKit/UIKit.h>
#interface CustomCell : UITableViewCell{
IBOutlet UIImageView *cellImageview;
IBOutlet UILabel *cellTitle;
IBOutlet UILabel *cellDescription;
IBOutlet UILabel *cellDate;
}
#property (retain, nonatomic) IBOutlet UIImageView *cellImageview;
#property (retain, nonatomic) IBOutlet UILabel *cellTitle;
#property (retain, nonatomic) IBOutlet UILabel *cellDescription;
#property (retain, nonatomic) IBOutlet UILabel *cellDate;
#end
CustomCell.m
#import "CustomCell.h"
#implementation CustomCell
#synthesize cellDate;
#synthesize cellDescription;
#synthesize cellImageview;
#synthesize cellTitle;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)dealloc {
[super dealloc];
}
#end
This is my both class
here Problem is in tableview data of the customcell cant Display onlt white screen.
Here i work in ios with masterdetails view template..
here i have also added m file in compile source
Customcell.xib size is 300X100
Here my output
My xib file as below
please help me to solve problem
if(cell == nil)
{
NSArray *outlets = [NSArray arrayWithArray:[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil]];
for(id obj in outlets)
{
if([obj isKindOfClass:[UITableViewCell class]])
{
cell = (CustomCell *)obj;
}
}
}
If your CustomCell is made via XIB then Write this Code where your cell is nil.
EDIT:-
This may be the cause - I think you haven't changed your CustomCell's class name. Follow these steps -
Take a XIB with name CustomCell.xib then delete its view.
Take a UITableViewCell and set its height and structure according to you.
Select File's Owner and change its class name to CustomCell, Same thing do with UITableViewCell... select it and change its class name to CustomCell.
Now connect all subView's IBOutLets.
Note:- Select IBOutLets by right clicking on UITableViewCell not from File's Owner.
Do the following in viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
UINib *nib = [UINib nibWithNibName:#"Customcell" bundle:nil];
[[self tableView] registerNib:nib forCellReuseIdentifier:#"CustomCell"];
}
If the cell is designed in a nib then you need to load the cell from the nib.
This actually has support in UIKit since iOS 5
What you need to do is register your nib with the UITableView.
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:#"Customcell" bundle:nil]
forCellReuseIdentifier:#"CustomCell"];
//...
}
Now your tableView:cellForRowAtIndexPath: just needs to look like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CustomCell";
CustomCell *cell = (id)[tableView dequeueReusableCellWithIdentifier: CellIdentifier];
cell.cellDate.text = #"date";
cell.cellDescription.text = #"Description";
cell.cellImageview.image = [UIImage imageNamed:#"facebook.png"];
cell.cellTitle.text = #"Title";
return cell;
}
NB
Make sure to remember to set CustomCell as the reuseIdentifier in the xib
Some other observations
Really you should be using ARC for a new project.
#synthesize is implicit so not required in most cases
It's probably a bad idea to #import "CustomCell.h" in MasterViewController.h.
You should move this to the .m and use a forward declaration in the .h instead - #class CustomCell;
You also do not need to declare the backing ivars for properties as these will also be generated by the compiler e.g. you don't need to declare the following
#interface CustomCell : UITableViewCell {
IBOutlet UIImageView *cellImageview;
IBOutlet UILabel *cellTitle;
IBOutlet UILabel *cellDescription;
IBOutlet UILabel *cellDate;
}
New to objective. I am trying to put 2 values into a table view. this is part of the code:
- (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.font = [UIFont systemFontOfSize:14]; //Change this value to adjust size
cell.textLabel.numberOfLines = 2;
NSString *desc= #"Alight Destination =";
cell.textLabel.text = desc,[alDescArray objectAtIndex:indexPath.row];
return cell;
}
for this line cell.textLabel.text = desc,[alDescArray objectAtIndex:indexPath.row];
the table only display Alight Destination = without adding in the values for [alDescArray objectAtIndex:indexPath.row];
What part did i do wrongly pls help
You should use
cell.textLabel.text=[NSString stringWithFormate:#"%# %#",desc,[alDescArray objectAtIndex:indexPath.row];
or
NSString *desc=[NSString stringWithFormart: #"Alight Destination = %#",[alDescArray objectAtIndex:indexPath.row] ];
cell.textLabel.text = desc;
`
Use following way...
cell.textLabel.text = [NSString stringWithFormat:#"%# %#",desc,[alDescArray objectAtIndex:indexPath.row]];
I think it will be helpful to you.
guess subclassing UITableViewCell will be useful. find code below hope it will help..!
# INTERFACE FILE
#import <UIKit/UIKit.h>
#interface CustomTableViewCell : UITableViewCell {
UIImageView *imageView;
UILabel *titleLabel1;
UILabel *titleLabel2;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;
- (void)setValuesForCell:(YourModal*)agent withIndexPath:(NSIndexPath*)indexPath;
#property(nonatomic,retain) UIImageView *imageView;
#property(nonatomic,retain) UILabel *titleLabel1;
#property(nonatomic,retain) UILabel *titleLabel2;
#end
#IMPLEMENTATION FILE
#implementation CustomTableViewCell
#synthesize titleLabel1,titleLabel2,imageView;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Your initialisation if any.
}
return self;
}
- (void)setValuesForCell:(YourModal*)modal withIndexPath:(NSIndexPath*)indexPath{
titleLabel1.text=modal.name;
titleLabel2.text=modal.description;
imageView.image=modal.image;
//Other values you want set from modal
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state.
}
- (void)dealloc {
[super dealloc];
[titleLabel1 release];
[titleLabel2 release];
//Other memory releases
}
#end
In your xib you can load the CustomTableViewCell as your class for viewing on tableview
My Custom View Cell looks like the following http://img34.imageshack.us/i/screenshot20110210at627.png/
#interface AddSiteAddressCell : UITableViewCell {
IBOutlet UITextField *street;
IBOutlet UITextField *city;
IBOutlet UITextField *province;
IBOutlet UITextField *postal;
IBOutlet UITextField *country;
IBOutlet UITextField *siteName;
}
#property (nonatomic,retain)UITextField *siteName;
#end
#implementation AddSiteAddressCell
- (void) textFieldDidEndEditing:(UITextField *)textField {
NSLog(#"%#",street);
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code.
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state.
}
- (void)dealloc {
[super dealloc];
}
#end
AddSiteViewController.m
-
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.section == 0)
{
static NSString *MyIndentifier = #"MyIdentifier";
MyIndentifier = #"tblCellView";
//static NSString *CellIdentifier = #"Cell";
AddSiteAddressCell *cell = (AddSiteAddressCell *)[add_site dequeueReusableCellWithIdentifier:MyIndentifier];
if(cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"AddSiteAddressCell" owner:self options:nil];
cell = nameCell;
}
return cell;
}else if (indexPath.section == 1)
{
static NSString *MyIndentifier = #"MyIdentifier";
MyIndentifier = #"tblCellView";
//static NSString *CellIdentifier = #"Cell";
AddSiteAddressCell *cell = (AddSiteAddressCell *)[add_site dequeueReusableCellWithIdentifier:MyIndentifier];
if(cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"AddSiteAddressCell" owner:self options:nil];
cell = addressCell;
}
return cell;
}
/*static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...*/
return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
AddSiteAddressCell *cell = (AddSiteAddressCell *)[add_site cellForRowAtIndexPath:[indexPath row]];
self.test = cell.siteName.text;
NSLog(#"%#",self.test);
}
MY full view looks like this:
http://img29.imageshack.us/i/screenshot20110211at940.png/
This is what my View looks like. 2 sections with 1 row each and 1 custom cell each
Thats code from my CustomTableViewCell
I am wondering how I can access my text field in my table controller to save the data that is entered
when do u want to access it?
you should make property for your text field (e.g siteName), and then if you have the IndexPath of your cell, u can use:
AddSiteAddressCell *myCell=(AddSiteAddressCell *)[tableView cellForRowAtIndexPath:MyIndexPath];
myCell.siteName....