I am having trouble with a UITableViewCell.
I have a view controller in my storyboard that is connected to my view controller called MainViewController. It Contains a UITableViewCell with 3 labels. The UITableViewCell is connected to the class MTTableViewCell.
// MTTableViewCell.h
#import <UIKit/UIKit.h>
#interface MTTableViewCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UILabel *mainLabel;
#property (strong, nonatomic) IBOutlet UILabel *statusLabel;
#end
// MTTableViewCell.m
#import "MTTableViewCell.h"
#implementation MTTableViewCell
- (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
// MainViewController.m
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
MTTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MTTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSString *namecell = #"namecell";
NSString *statuscell = #"statuscell";
[cell.mainLabel setText:namecell];
[cell.statusLabel setText:statuscell];
return cell;
}
The problem is that nothing is shown when I run the MainViewController. What am I missing? I am not getting any arrors, just an empty tableview with 1 blank record.
Look in your interface builder - you shuld have not "conntected" your custom cell, you should set it as a "files owner" since you implemented the view, not want to set a delegate. Maybe this hint helps?
You are not loading cell from main Bundle :
Use Below code this will work for you :
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = #"FleetAccountCell";
FleetAccountCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:#"FleetAccountCell" owner:nil options:nil] objectAtIndex: 0];;
}
return cell;
}
Try looking here ... the answer to your problem could be
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object{
CustomCell *cell = (CustomCell * )[self.tableView dequeueReusableCellWithIdentifier:#"YOUR CELL NAME" forIndexPath:indexPath];
// your content cell...
return cell;
}
Read it here PFQueryTableViewController not showing custom cells
Greetings :)
This work for me..
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = #"cellID";
FleetAccountCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:#"Your_nibName" owner:nil options:nil] objectAtIndex: 0];
}
[cell layoutIfNeeded];
return cell;
}
Declare TableViewcell this way :
TableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
Below one was not working for me :
TableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
Rewrite you cellForRowAtIndexPath: method as below
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
MTTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:#"CUSTOME_CELL_Nib_NAME" owner:nil options:nil] objectAtIndex: 0];;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSString *namecell = #"namecell";
NSString *statuscell = #"statuscell";
[cell.mainLabel setText:namecell];
[cell.statusLabel setText:statuscell];
return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"cell";
TableViewCell *cell = (TableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"TableViewCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
I had the same problem, I was missing a connection in the tableview delegates and datasource:
self.yourtableviewname.delegate=self;
self.youtableviewname.datasource=self;
After that it worked fine.
Select the cell and in the inspector tab,check mark "installed" and it will work.
You didn't instantiate mainLabel and statusLabel,so they are nil right now.You must instantiate them in the initWithStyle method.
Related
I'm using Objective-C. I want to push a view controller with a table view in it. When I use a normal table view cell, it works well. But when I use custom cells, It couldn't load and the simulator freeze.
Is there any problem can cause the simulator freeze when push a view controller? Some one can help me?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *simpleIdentifier = #"comment";
commentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleIdentifier];
if (cell == nil){
cell = [[commentTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleIdentifier];
}
cell.username.text = [comments[indexPath.row] objectForKey:#"user"];
cell.textLabel.text = [comments[indexPath.row] objectForKey:#"content"];
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *customTableIdentifier=#"CustomCell";
CustomCell *cell=(CustomCell*)[tableView dequeueReusableCellWithIdentifier:customTableIdentifier];
if (cell==nil)
{
NSArray *nibs=[[NSBundle mainBundle]loadNibNamed:#"CustomCell" owner:self options:nil];
cell=[nibs objectAtIndex:0];
}
cell.yourLabelData.text = #"iOS";
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) //If you want to go view controller from particular index to other View Controller
{
//Here write the code for push the view
}
}
#pragma mark TableView delegete methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [Array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier=#"cellIdentifier";
SocialTableViewCell *cell=(SocialTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SocialTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
//write code for display data
return cell;
}
You need to register your custom cell in viewdidload
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[tableView registerNib:[UINib nibWithNibName:#"myCustomCell" bundle:nil] forCellWithReuseIdentifier:#"myCell"];
}
try these way..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *cellidentifier=[NSString stringWithFormat:#"%ld",(long)indexPath.row];
CustomCell *cell= (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellidentifier];
if (cell==nil)
{
cell=[[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
if (indexPath.row==0)
{
//Here change or edit specific custom cell if u want like i have UIImageview and i want to give it a specific image..
[cell.imgView1 setImage:imgsp1];
}
return cell;
}
i hope it helps..
in ViewController
.h
#property (strong, nonatomic) IBOutlet UITableView *tableViewData;
.m
#import "ViewController.h"
#import "CustomCell.h"
#interface ViewController ()
{
NSDictionary *dictComments;
NSArray *arrayUser;
NSArray *arrayContents;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
dictComments = [[NSDictionary alloc]initWithObjectsAndKeys:#"Jin",#"User",#"iOS Developer",#"Content", nil];
arrayUser = [[NSArray alloc]initWithObjects:[dictComments objectForKey:#"User"], nil];
arrayContents = [[NSArray alloc]initWithObjects:[dictComments objectForKey:#"Content"], nil];
}
//UITableViewDataSource Methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arrayUser.count;
//OR
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *customTableIdentifier=#"CustomCell";
CustomCell *cell=(CustomCell*)[tableView dequeueReusableCellWithIdentifier:customTableIdentifier];
if (cell==nil)
{
NSArray *nibs=[[NSBundle mainBundle]loadNibNamed:#"CustomCell" owner:self options:nil];
cell=[nibs objectAtIndex:0];
}
cell.labelUserName.text = [NSString stringWithFormat:#"%#",[arrayUser objectAtIndex:indexPath.row]];
cell.labelTextContent.text = [NSString stringWithFormat:#"%#",[arrayContents objectAtIndex:indexPath.row]];
//OR
cell.labelUserName.text = [NSString stringWithFormat:#"%#",[dictComments objectForKey:#"User"]];
cell.labelTextContent.text = [NSString stringWithFormat:#"%#",[dictComments objectForKey:#"Content"]];
return cell;
}
was working in Xcode 4.6 and have a UItableview set up where the cells are populated from an array. When working in 4.6, the view behaved perfectly and was just how I liked it. After the update to 5.1.1, scrolling seems to have been enabled on the table view, the table view is loaded from the bottom of the view and the 3 populated cells at the top are not visible. Sometimes the view will rubber band and not allow me to scroll all the way back up to the top, and sometimes it won't rubber band and will let me. I am fairly new to this, but have tried messing around with auto layout, but to no avail.
Here's my code for the .h
#interface TableViewController : UITableViewController
#property (nonatomic, strong) NSArray *Manufacturers;
#end
Here's my code for the .m:
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_Manufacturers = #[#"1",
#"2",
#"3"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return _Manufacturers.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TableCell";
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
int row = [indexPath row];
cell.TitleLabel.text = _Manufacturers[row];
return cell;
}
In cellForRowAtIndexPath:
If you are using default UITableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TableCell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MyIdentifier"];
}
int row = [indexPath row];
cell.textLabel.text = _Manufacturers[row];
return cell;
}
If you are using custom TableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.nameLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
TableCell is Custom than write below code..
TableCell *cell = (TableCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"TableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
Otherwise
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
Look at this tutorial for custom cell
http://www.appcoda.com/customize-table-view-cells-for-uitableview/
I want to use a custom reusable UITableViewCell with my UItableview, it works but, testing with instruments - allocation, I see that memory is not released when I get out of my UIViewController and re-enter several times.
here are the three methods that i've tried,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// method #1
static NSString *CellIdentifier2 = #"customClassicCell";
customClassicCell *cell = (customClassicCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell == nil){
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"customClassicCell" owner:nil options:nil];
for(id currentObject in topLevelObjects)
{
if([currentObject isKindOfClass:[customClassicCell class]])
{
cell = (customClassicCell *)currentObject;
break;
}
}
}
return cell;
}
or
static NSString *cellIdentifier44 = #"custom44";
- (void)viewDidLoad {
[self.tbv registerNib:[UINib nibWithNibName:#"customClassicCell" bundle:nil] forCellReuseIdentifier:cellIdentifier44];
[super viewDidLoad];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// method #2
customClassicCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier44];
return cell;
}
or
// method3
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier44];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier44] ;
}
return cell;
}
instrument shows this (every times I get out and get in my UIviewcontroller), it's the same problem with the 3 methods, memory is not correctly released.
I use xcode 5.0 target 5.1
I do not use ARC neither storyboards. Anyone has an idea ?
i'm trying to do a selection list in UITableView but whenever i tap in a row, two row are marked. For example if i tap the first row it adds a checkmark to row = 0 and row = 11. I think the problem is caused by reuse of cells but i don't understand how solve it.
I attach my code .h:
#interface tabellaViewController : UITableViewController
#property (nonatomic, strong) NSArray *listaingredienti;
#end
and my code .m:
- (void)viewDidLoad
{
[super viewDidLoad];
_listaingredienti = [NSArray arrayWithObjects:#"formaggio", #"crudo", #"pomodorini", #"fossa", #"bresaola", #"funghi", #"salame", #"fontina", #"rucola", #"radicchio", #"peperoni", #"ciccioli", #"squacquerone", #"gorgonzola", #"salsiccia", #"cipolla", #"feta", #"acciughe", #"pecorino", nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_listaingredienti count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [_listaingredienti objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow]
animated:NO];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
I've seen UITableView didSelectRowAtIndexPath add additional checkmark at tap where there is a similar post but i already use dequeueReusableCellWithIdentifier:CellIdentifier.
Anyone can help me?
Ok,
i'm just solved my question. Actually, the problem was in reuse of cells, but this code fix it.
Just replace in cellForRowAtIndexPath::
static NSString *CellIdentifier = #"Cell";
with:
NSString *CellIdentifier = [NSString stringWithFormat:#"MyReuseIdentifier %d",indexPath.row];
I searched too much, tried lot of thinks but i couldn't make it work.
I have a view controller, and in that controller i have a tableview which has custom cell.
I connect outlets,datasource and delegate but when i run the project, instead of my custom cells. UITableCell is shown and not showing data either. here is my .h and .m files
#import <UIKit/UIKit.h>
#class SelectMutantCell;
#interface MutantViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>{
UITableView *tableView;
IBOutlet SelectMutantCell *mutantCell;
}
#property (nonatomic, weak) IBOutlet UITableView *tableView;
#property (nonatomic, weak) SelectMutantCell *mutantCell;
- (IBAction)cancel:(id)sender;
#end
and here is my .m file
#implementation MutantViewController
#synthesize tableView = _tableView;
#synthesize mutantCell = _mutantCell;
NSArray *mutants;
- (void)viewDidLoad
{
[super viewDidLoad];
//mutants = [mutants initWithObjects:#"Keko",#"HEHE",#"PEPE", nil];
UINib *cellNib = [UINib nibWithNibName:#"SelectMutantCell" bundle:nil];
[self.tableView registerNib:cellNib forCellReuseIdentifier:#"SelectMutantCell"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"SelectMutantCell";
SelectMutantCell *cell = (SelectMutantCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSLog(#"---");
cell.nameLabel.text = #"Hehe"];
cell.descriptionLabel.text = #"hhe";
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 3;
}
the NSLog(#"---") is for debugging and it appears when running. the problem is in
cell.nameLabel.text = #"Hehe"];
cell.descriptionLabel.text = #"hhe";
this block, if i write cell.textLabel.text = "test" it works but i need my custom cell to shown.
any help will be appreciated..
Insert if(cell==nil){} loop
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"SelectMutantCell";
SelectMutantCell *cell = (SelectMutantCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[SelectMutantCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSLog(#"---");
cell.nameLabel.text = #"Hehe"];
cell.descriptionLabel.text = #"hhe";
return cell;
}
Looking at your cellForRowAtIndexPath: method, you never initialize the cell. Try doing it like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"SelectMutantCell";
SelectMutantCell *cell = (SelectMutantCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"SelectMutantCell" owner:nil options:nil];
for(id currentObject in topLevelObjects) {
if([currentObject isKindOfClass:[SelectMutantCell class]]) {
cell = (SelectMutantCell *)currentObject;
break;
}
}
}
NSLog(#"---");
cell.nameLabel.text = #"Hehe"];
cell.descriptionLabel.text = #"hhe";
return cell;
}
I had the same issue. To solve it check the followings :
in your nib file, you have removed the root view created by default, and dragged a UITableViewCell from XCode object browser, then set the custom type of the UItableViewCell to your subclass SelectMutantCell
you have linked the outlets
you have added the cell identifier to SelectMutantCell, as you would do in storyboard
last but not least, you didn't add the same cell in your storyboard TableView, it will conflit between the one from storyboard and the one from [self.tableView registerNib:cellNib forCellReuseIdentifier:#"SelectMutantCell"];