I have created a UITableViewCell and I am trying to display it in one of my table, but for some reason it doesn't come, the table is coming empty. And when I click on the one of cell in the table it takes me down to next level, which means the custom UITableViewCell is not getting rendered properly.
Here is my custom UITableViewCell .h file
#interface ProductViewCell : UITableViewCell {
IBOutlet UILabel *titleLabel;
IBOutlet UILabel *detailLabel;
IBOutlet UIImageView *imageView;
}
#property (nonatomic, retain) UILabel *titleLabel;
#property (nonatomic, retain) UILabel *detailLabel;
#property (nonatomic, retain) UIImageView *imageView;
Here is the .m file
#import "ProductViewCell.h"
#implementation ProductViewCell
#synthesize titleLabel, detailLabel, imageView;
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
#end
Here is the .h file which subclasses UITableViewController
#interface ProductCategoryViewController : UITableViewController {
NSArray *tableDataSource;
}
#property (nonatomic, retain) NSArray *tableDataSource;
Here is the .m file for ProductCategoryViewController
#implementation ProductCategoryViewController
#synthesize tableDataSource;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
ProductViewCell *cell = (ProductViewCell*)[tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[ProductViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
cell.titleLabel.text = [dictionary objectForKey:#"name"];
cell.imageView.image = [UIImage imageNamed:[dictionary objectForKey:#"imageUrl"]];
return cell;
}
Your help is appreciated.
Thanks,
Yogesh
I got this working by making some changes, in my tableviewcontroller I added an IBOutlet for ProductViewCell and synthesis the same.
Then I changed the xib file for ProductViewCell
1) I make File's Owner to my tableViewController
2) Then here I linked the the IBOutlet that I created in the tableViewContoller to the ProductViewCell.
I am not sure if this is the right way to do it but atleast this is working for me now
Related
I've never implemented a custom cell for a tableView before and am confused on how to proceed. I'm trying to make a personality test app where each question is represented in a cell of my tableView and underneath each question are several buttons that will act as a response to the corresponding question. I've managed to set up my cell with a textview for the question and several buttons with appropriate constraints so they won't move.
I also set the Cell style to custom in the attributes inspector.
Can someone help me with how to set up the textview with my question? I tried making a property of UITextView and linking it to TextView in the cell but then an error arose saying I wasn't allowed to populate a reoccurring cell with an IBOutlet.
Here is a screenshot of my storyboard. Let me know if you need to see anything else.
TestVC.h
#import <UIKit/UIKit.h>
#import "Questions.h"
#import "CustomCell.h"
#interface TestViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (strong, nonatomic) IBOutlet UITableView *tableView;
#property (strong, nonatomic) IBOutlet UITextView *instructions;
#property (weak, nonatomic) IBOutlet UIButton *results;
//#property (strong, nonatomic) IBOutlet *question;
#property (nonatomic, strong) Questions *survey;
-(IBAction)backPressed:(id)sender;
#end
TestViewController.m
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [survey.questions count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"QuestionCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
/*
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
*/
NSString *que = (NSString *)[survey.questions objectAtIndex:indexPath.row];
//UITextView *textView = (UITextView *)[cell viewWithTag: 100];
cell.textView = (UITextView *)[cell viewWithTag: 100];
cell.textView.text = que;
return cell;
}
CustomCell.h
#import <UIKit/UIKit.h>
#interface CustomCell : UITableViewCell
// Variables for questions and answers
#property (weak, nonatomic) IBOutlet UITextView *textView;
#property (weak, nonatomic) IBOutlet UIButton *strongAgree;
#property (weak, nonatomic) IBOutlet UIButton *agree;
#property (weak, nonatomic) IBOutlet UIButton *weakAgree;
#property (weak, nonatomic) IBOutlet UIButton *neutral;
#property (weak, nonatomic) IBOutlet UIButton *strongDisagree;
#property (weak, nonatomic) IBOutlet UIButton *disagree;
#property (weak, nonatomic) IBOutlet UIButton *weakDisagree;
#end
CustomCell.m
#import "CustomCell.h"
#interface CustomCell ()
#end
#implementation CustomCell
#synthesize textView;
#synthesize strongAgree;
#synthesize agree;
#synthesize weakAgree;
#synthesize neutral;
#synthesize strongDisagree;
#synthesize disagree;
#synthesize weakDisagree;
#end
Set a tag for the UITextView in the attributes inspector and get it by the viewWithTag Method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"QuestionCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSString *que = (NSString *)[survey.questions objectAtIndex:indexPath.row];
UITextView *textView = (UITextView *)[cell viewWithTag:100];//Use your tag
textView.text = que;
return cell;
}
this should work for you.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"QuestionCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSString *que = (NSString *)[survey.questions objectAtIndex:indexPath.row];
cell.textLabel.text = que;
return cell;
}
From what I see here, you doesn't create a custom cell, just a normal UITableViewCell
You need to create a subclass of UITableViewCell. Call it CustomCell or whatever, then give it the IBoutlet properties that you created in your storybard.
For exemple, in CustomCell.h :
#property (weak, nonatomic) IBOutlet UITextView textView;
Then in the cellForRowAtIndexPath :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"QuestionCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
// EDIT
if (!cell)
{
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}//
NSString *que = (NSString *)[survey.questions objectAtIndex:indexPath.row];
cell.textview.text = que;
return cell;
}
Finally in you storyboard, select the cell you customized, and setup its class in the inspector as CustomCell.
NB : Your IBOutlet properties should always be weak properties.
i have a UITableView in a storyboard in an ios xcode project,
i also have an array of menuitem objects stored in itemarray:
NSMutableArray *itemarray;
this is my menuitem.h file
#import <Foundation/Foundation.h>
#interface menuitem : NSObject
#property (nonatomic) NSString *itemtitle;
#property (nonatomic) NSString *itemdesc;
#property (nonatomic) int itemid;
#property (nonatomic) int itemprice;
#property (nonatomic) NSString *itemcat;
#end
this is my tableviewcell.m file:
#import "tableviewcell.h"
#import "menuitem.h"
#interface tableviewcell ()
#property (nonatomic, weak) IBOutlet UILabel *titleLabel;
#property (nonatomic, weak) IBOutlet UILabel *descLabel;
#property (nonatomic, weak) IBOutlet UILabel *priceLabel;
#end
#implementation tableviewcell
-(void)configureWithmenuitem:(menuitem *)item{
NSLog(#"hello");
self.titleLabel.text = item.itemtitle;
self.descLabel.text = item.itemdesc;
self.priceLabel.text = [NSString stringWithFormat:#"%.1d", item.itemprice];
}
#end
i want to get the itemtitle, itemdesc and itemprice from each instance of menuitem into 3 labels on each of the tableviewcells
Your tableviewcell is a View and thus it should only know how to display itself and not what to display. It's the job of your controller (through the delegate tableView:cellForRowAtIndexPath: method) to tell the view what to display.
Thus you should do something like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Id"];
//Assuming you've stored your items in an array.
Item *item = [items objectAtIndex:indexPath.row];
cell.titleLabel.text = item.itemTitle;
cell.descLabel.text = item.itemdesc;
cell.priceLabel.text = [NSString stringWithFormat:#"%.1d", item.itemprice];
return cell;
}
^ The above code assumes you have a prototype cell. If you don't, you'll have to alloc if cell is nil after dequeue.
I am trying to create a table with prototype cell
for that i had taken a tableview and inserted a cell in table view
and in cell i had taken a label in which i want to set values from my Employee class.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
RWTCell *cell = (RWTCell*)[tableView dequeueReusableCellWithIdentifier:#"employeeeCell"];
if (!cell){
cell = [[RWTCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"employeeeCell"];
}
Employee *currentEmp=[employeeArray objectAtIndex:indexPath.row];
NSLog(#" %#",currentEmp.empName);
cell.lName.text=currentEmp.empName;
// cell.textLabel.text = currentEmp.empName;
return cell;
}
Above is my code
when i try to do
RWTCell *cell = (RWTCell*)[tableView dequeueReusableCellWithIdentifier:#"employeeeCell"];
it always gives me null, and this line below never works
cell.lName.text=currentEmp.empName;
my Employee.m class
#import "Employee.h"
#implementation Employee
#synthesize empDepart,empDoj,empDob,empName,empSalary,empGender;
#end
and Employee.h
#import <Foundation/Foundation.h>
#interface Employee : NSObject
#property (nonatomic, retain) NSString *empName;
#property (nonatomic, retain) NSString *empSalary;
#property (nonatomic, retain) NSString *empDob;
#property (nonatomic, retain) NSString *empDoj;
#property (nonatomic, retain) NSString *empGender;
#property (nonatomic, retain) NSString *empDepart;
#end
EDIT
sorry i forgot to mention my cell class
and i am working on storyboard
#import "RWTCell.h"
#implementation RWTCell
#synthesize lName;
- (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
}
#end
#import <UIKit/UIKit.h>
#interface RWTCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UILabel *lName;
#end
Take a look at the custom cell I am using in my application.
.h file of custom cell
#import <UIKit/UIKit.h>
#interface EvalHistoryCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UILabel *lblEvaluator;
#property (strong, nonatomic) IBOutlet UILabel *lblDate;
#property (strong, nonatomic) IBOutlet UILabel *lblStore;
#property (strong, nonatomic) IBOutlet UILabel *lblStatus;
#end
.m file of custom cell
#import "EvalHistoryCell.h"
#implementation EvalHistoryCell
- (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
}
#end
in main viewcontroller where you want to use it:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *strEvalIdentifier = #"HistoryIdentifier";
EvaluationListEntity *objEvalEntity=[arrmEvalList objectAtIndex:indexPath.row];
EvalHistoryCell *cell = (EvalHistoryCell *)[tableView dequeueReusableCellWithIdentifier:strEvalIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"EvalHistoryCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.lblEvaluator.text=objEvalEntity.strEvaluatorName;
return cell;
}
Try this.
Make sure you connected your custom cell with your view at RWTCell.xib
At your .h file
#property(nonatomic,strong) RWTCell *rwtcell;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier= #"MyIdentifier";
RWTCell *cell = (RWTCell*)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(cell==nil)
{
[[NSBundle mainBundle] loadNibNamed:#"RWTCell" owner:self options:nil];
cell=self.rwtcell;
}
Employee *currentEmp=[employeeArray objectAtIndex:indexPath.row];
NSLog(#" %#",currentEmp.empName);
cell.lName.text=currentEmp.empName;
return cell;
}
Hope it helps you..
I have a custom PFTableViewCell using a NIB. All of my custom labels display, but my PFImageView does not. I changed everything to a UIImageView just to make sure it wasn't something wrong with my Parse call, but I couldn't get it to work even with setting a static image in my resources.
My cell's class is set as foodCell and foodcell.h is set as the owner.
Here is a screenshot showing the imageview on my NIB
Here is my foodCell.h to show the outlet of the PFImageView :
#import <Parse/Parse.h>
#interface foodCell : PFTableViewCell
#property (weak, nonatomic) IBOutlet NSObject *foodCell;
#property (weak, nonatomic) IBOutlet UILabel *priceLabel;
#property (weak, nonatomic) IBOutlet UILabel *foodDescription;
#property (weak, nonatomic) IBOutlet UILabel *foodTitle;
#property (strong, nonatomic) IBOutlet PFImageView *foodPic;
#end
Now here is where I configure my cell. Again, all of my other code here works, except the foodCell.foodPic.file :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = #"foodCell";
foodCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[foodCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.foodPic.file = [object objectForKey:#"foodImage"];
cell.foodTitle.text = [object objectForKey:#"foodName"];
cell.foodDescription.text = [object objectForKey:#"foodDescription"];
NSString *myString = [#"$" stringByAppendingString:[NSString stringWithFormat:#"%1#", [object objectForKey:#"foodPrice"]]];
cell.priceLabel.text = myString;
return cell;
}
As per Parse documentation, you should call loadInBackground for that PFImageView
cell.foodPic.file = [object objectForKey:#"foodImage"];
[cell.foodPic loadInBackground];
Also call [PFImageView class] in your AppDelegate.m didFinishLaunchWithOptions: method.
source: https://parse.com/questions/using-pfimageview-with-storyboard
I made a custom cell with a XIB:
.h
#import <UIKit/UIKit.h>
#interface TWCustomCell : UITableViewCell {
IBOutlet UILabel *nick;
IBOutlet UITextView *tweetText;
}
#end
.m
#import "TWCustomCell.h"
#implementation TWCustomCell
- (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
}
#end
And I load them in cellForRowAtIndexPath: in this way:
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
TWCustomCell *cell = (TWCustomCell*)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObject = [[NSBundle mainBundle] loadNibNamed:#"TWCustomCell" owner:nil options:nil];
for (id currentObject in topLevelObject) {
if([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (TWCustomCell*) currentObject;
break;
}
}
}
// Configure the cell...
cell.tweetText.text = [tweets objectAtIndex:indexPath.row];
return cell;
}
On cell.tweetText.text = [tweets objectAtIndex:indexPath.row];
On the dot after cell, Xcode tells me_ "Property 'tweetText' not found on object of type 'TWCustomCell *'; did you mean to access ivar 'tweetText'?" and tells me to replace it with
cell->tweetText.text. But there appears the error: "Semantic Issue: Instance variable 'tweetText' is protected". What do I have to do?
You didn't declare a property that will allow access to the IBOutlets outside of the class with the dot syntax.
Here's how i would do it:
in your .h file:
#property (nonatomic, readonly) UILabel *nick;
#property (nonatomic, readonly) UITextView *tweetText;
in the .m:
#synthesize nick, tweetText;
Or you could remove the ivar IBOutlets and declare the properties as retain and IBOutlets like this:
#property (nonatomic, retain) IBOutlet UILabel *nick;
#property (nonatomic, retain) IBOutlet UITextView *tweetText;
The problem was with my custom cell ivars:
#import <UIKit/UIKit.h>
#interface TWCustomCell : UITableViewCell {
//added here #public and you can access them now
#public
IBOutlet UILabel *nick;
IBOutlet UITextView *tweetText;
}
#end