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;
}
Related
I am new to iOS and I am trying to build an lightweight app for practice. I am taking two textfield entries from one view controller and storing them in NSDictionary. I am transferring those data in NSArray. Now I want to display those data from NSArray to table view. I am using this line of code:
cell.textLabel.text=[self.myArray objectAtIndex:indexPath.row];
But my app is crashing when cursor comes at this line. Help is appreciated.Thanks in advance.
For UITableView you have to define at least 2 methods:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
It seems your method numberOfRowsInSection is not implemented.
Try to add this:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
{
return [self.myArray count];
}
Of course your ViewController have to be delegated for UITableViewDataSource and UItableViewDelegate.
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.yourArry count];
}
-(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];
}
//Configure Cell
cell.textLabel.text = self.yourArry[indexPath.row];
return cell;
}
Please try this code
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
{
return [self.myArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell=nil;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text =[self.myArray objectAtIndex:indexPath.row]
return cell;
}
Trying using a pointer to an NSString (or what object you are trying to make)
NSString *string = [self.myArray objectAtIndex:indexPath.row]
Make sure you have the count of cell in the tableview set like so
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
{
return self.myArray.count
}
Step One.
In YourViewController.h
#import <UIKit/UIKit.h>
#interface YourViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
In YourViewController.m
#import "YourViewController.h"
#interface YourViewController ()
{
UITableView * YourTableview;
}
- (void)viewDidLoad
{
YourTableview = [[UITableView alloc] initWithFrame:CGRectMake(0,0, 300, 500) style:UITableViewStylePlain];
YourTableview.delegate=self;
YourTableview.dataSource=self;
[self.view addSubview:YourTableview];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * CellIdentifier=#"Cellid";
UITableViewCell * SoftCell=[YourTableview dequeueReusableCellWithIdentifier:CellIdentifier];
if (!SoftCell)
{
SoftCell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
textLabel.text = [NSString stringWithFormat:#"%ld",(long)indexPath.row];
return SoftCell;
}
My custom cell is currently displaying incorrectly (there on top of each other and with no background):
My TableViewController is currently configured like this:
#import "EditExerciseTableViewController.h"
#import "ExerciseSetCell.h"
#interface EditExerciseTableViewController ()
#end
#implementation EditExerciseTableViewController
static NSString *CellIdentifier = #"ExerciseSetCell";
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:#"ExerciseSetCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
self.title = _routineExercise.exercise.name;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_routineExercise.sets count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ExerciseSetCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[ExerciseSetCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.setNumberTextLabel.text = [NSString stringWithFormat:#"%ld", (long)indexPath.row + 1];
return cell;
}
#end
Any ideas?
EDIT Appears to be a bug in iOS 8/XCode 6 beta 6
Try this. Hope it will helps you. and remove this line [self.tableView registerNib:[UINib nibWithNibName:#"ExerciseSetCell" bundle:nil] forCellReuseIdentifier:CellIdentifier]; from
viewDidLoad. also import your custom cell file #import"ExerciseSetCell.h"
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"ExerciseSetCell";
ExerciseSetCell *cell = (ExerciseSetCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ExerciseSetCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.setNumberTextLabel.text = [NSString stringWithFormat:#"%ld", (long)indexPath.row + 1];
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 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.
My Problem is, When I select a cell it automatically select other cell also.
Also when I deselect that selected cell, automatically selected cell also be deselect.
Any help is much appreciated.
Thank you.
My Code is :
#pragma mark
#pragma mark UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [cellDataArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = #"customCellSearchBySpeciality";
SpecialityCustomCell *cell = (SpecialityCustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SearchBySpecialityViewController" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.cellData.text = [cellDataArray objectAtIndex:indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
SpecialityCustomCell *selectedCell = (SpecialityCustomCell *)[tableView cellForRowAtIndexPath:indexPath];
if (selectedCell.selectedCellImageView.image) {
selectedCell.selectedCellImageView.image = nil;
}
else{
[selectedCell.selectedCellImageView setImage:[UIImage imageNamed:#"image"]];
}
}
I think you need to maintain instances of which cell you had aaded on each row then do selection and deselection on that particular object of cell's imageview like below,
NSMutableArray *cellArray;
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = #"customCellSearchBySpeciality";
SpecialityCustomCell *cell = (SpecialityCustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
[cellArray addObject:cell];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SearchBySpecialityViewController" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.cellData.text = [cellDataArray objectAtIndex:indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
for(int cc=0;cc<[cellArray count];cc++){
if(indexPath.row==cc){
if ([cellArray objectAtIndex:cc].selectedCellImageView.image) {
[cellArray objectAtIndex:cc].selectedCellImageView.image = nil;
}
else{
[[cellArray objectAtIndex:cc].selectedCellImageView setImage:[UIImage imageNamed:#"image"]];
}
}
}