- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is not being called - ios

So its really weird... I have a ViewController called ListEventsViewController
in the header file:
#interface ListEventsViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
#property (weak, nonatomic) IBOutlet UITableView *eventsTable;
#property (strong, nonatomic) NSArray *events;
#end
In the Implementation I am calling the pretty much mandatory functions below:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [events count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = nil;
cell = [eventsTable dequeueReusableCellWithIdentifier:#"eventCell"];
if(!cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"eventCell"];
}
NSString *textLabelTitle = [[events objectAtIndex:indexPath.row] objectForKey:#"name"];
NSString *textLabelDetail = [[events objectAtIndex:indexPath.row] objectForKey:#"date"];
//cell.imageView.image = someimage;
NSLog(#"Text Label Title: %#", textLabelTitle);
NSLog(#"Text Label Detail: %#", textLabelDetail);
cell.textLabel.text = textLabelTitle;
cell.detailTextLabel.text = textLabelDetail;
return cell;
}
The problem is the function - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ is not being called so the cells arent being populated. also the number of cells isnt being allocated either. What could be causing this?
It looks wired up to me heres a picture of the h and IB.

You illustrated in your screenshot that you have linked the table to its IBOutlet, but you haven't linked the table view's delegate or datasource to your controller and therefore your tables delegate methods such as cellForRowAtIndex will not be called. Linking these should fix your problem.

I have this & this must b your solution
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
mysampleimage = [[UIImageView alloc]initWithFrame:CGRectMake(3,3, 40, 40)];
mysampleimage.image=[UIImage imageNamed:#"a.png"];
mysampleimage.tag=13;
[cell.contentView addSubview:mysampleimage];
myButton = [[UIButton alloc]init];
myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(165, 10, 65, 30.);
[myButton setTitle:#"Show" forState:UIControlStateNormal];
myButton.tag = 14;
[cell addSubview:myButton];
lbl = [[UILabel alloc]initWithFrame:CGRectMake(50, 6, 200, 43)];
lbl.tag=12;
[cell.contentView addSubview:lbl];
}
else
{
lbl=(UILabel *)[cell.contentView viewWithTag:12];
mysampleimage=(UIImageView *)[cell.contentView viewWithTag:13];
myButton = (UIButton *)[cell.contentView viewWithTag:14];
}
myButton.accessibilityLabel = lbl.text;
[myButton addTarget:self action:#selector(myShow:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}

You can use the following lines in your coding:
tableview.delegate = self;
tableview.datasource = self;

What helped me was to comment out
numberofsectionsintableview

Related

change background color of UITableViewCell on tap on UIButton inside

I have a custom UITableViewCell who contains 3 UIButtons and I want that when I tap one button, the background of my cell change. But when I tap a button all cells in my UITableView change their background and not only the cell who has the button inside it.
Custom Cell (.h)
#interface SCActionsViewCell : UITableViewCell
#property (nonatomic, weak) UITableView *tableView;
#property (nonatomic, strong) NSIndexPath *indexPath;
#property (nonatomic, strong) UIButton *thanksButton;
#property (nonatomic, strong) UIButton *commentsButton;
#property (nonatomic, strong) UIButton *reButton;
#end
Custom Cell (.m)
#define TEXT_BUTTON_SIZE 25
#implementation SCActionsViewCell
- (void)awakeFromNib {
// Initialization code
self.backgroundColor = [UIColor colorLight];
_thanksButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width/3, actionsCellHeight)];
_commentsButton = [[UIButton alloc] initWithFrame:CGRectMake(self.contentView.frame.size.width/3, 0, self.contentView.frame.size.width/3, actionsCellHeight)];
_reButton = [[UIButton alloc] initWithFrame:CGRectMake(self.contentView.frame.size.width*2/3, 0, self.contentView.frame.size.width/3, actionsCellHeight)];
_thanksButton.titleLabel.font = [UIFont fontWithName:kFontAwesomeFamilyName size:TEXT_BUTTON_SIZE];
_commentsButton.titleLabel.font = [UIFont fontWithName:kFontAwesomeFamilyName size:TEXT_BUTTON_SIZE];
_reButton.titleLabel.font = [UIFont fontWithName:kFontAwesomeFamilyName size:TEXT_BUTTON_SIZE];
[_thanksButton setTitle:[NSString fontAwesomeIconStringForEnum:FAThumbsOUp] forState:UIControlStateNormal];
[_commentsButton setTitle:[NSString fontAwesomeIconStringForEnum:FACommentsO] forState:UIControlStateNormal];
[_reButton setTitle:[NSString fontAwesomeIconStringForEnum:FARetweet] forState:UIControlStateNormal];
[_thanksButton setTitleColor:[UIColor sGreen] forState:UIControlStateSelected];
[_reButton setTitleColor:[UIColor sGreen] forState:UIControlStateSelected];
[self addSubview:_thanksButton];
[self addSubview:_commentsButton];
[self addSubview:_reButton];
}
#end
SCViewDataSource (TableviewDataSource)
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.posts.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
SCActionsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"SCActionsViewCell" forIndexPath:indexPath];
cell.tableView = tableView;
cell.indexPath = indexPath;
[cell.thanksButton addTarget:(SCViewController *)tableView.delegate action:#selector(touched:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
SCViewController.m
-(void)touched:(UIButton *)button{
NSLog(#"indexPath : %#",((SCActionsViewCell *)button.superview).indexPath);
((SCActionsViewCell *)button.superview).backgroundColor = [UIColor blueColor];
}
In this case when I tap thanksButton in a cell all the cells change their background to blueColor...
(Try this) Updated you touched methood using following code.
-(void)touched:(UIButton *)button{
NSLog(#"indexPath : %#",((SCActionsViewCell *)button.superview).indexPath);
((SCActionsViewCell *)button.superview).contentView.backgroundColor = [UIColor blueColor];
}
Ok I found out by my own : don't use [tableView dequeueReusableCellWithIdentifier:forIndexPath:] but [[CustomCell alloc] init] and create your init function in your custom cell class.
Like this every cells will be a new object.
Add a property (maybe an NSDictionary or an NSArray) for tracking which indexPaths have been tapped already. Creating a new cell every time is very expensive. I suggest you don't abandon [tableView dequeueReusableCellWithIdentifier:forIndexPath:].
You could try this:
- (void)touched:(UIButton *)sender {
NSIndexPath *indexPath = ((SCActionsViewCell *)button.superview).indexPath;
[self.thankedIndexPaths addObject:indexPath];
}
Then you can set the background color accordingly:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
SCActionsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"SCActionsViewCell" forIndexPath:indexPath];
cell.tableView = tableView;
cell.thanked = [self.thankedIndexPaths containsObject:indexPath];
cell.indexPath = indexPath;
[cell.thanksButton addTarget:(SCViewController *)tableView.delegate action:#selector(touched:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}

UILabel in TableCell never changes font or colour

I've been trying to find a solution for this for quite some time now but with no luck. I've got a TableViewController with a number of Cells, all of which have a UILabel with some text. I'm trying to change the font and colour with the following code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.font = [UIFont fontWithName:#"Times New Roman" size:12.0f];
cell.textLabel.textColor = [UIColor redColor];
return cell;
}
This code should be correct to set the font and colour to a UILabel but it just never changes. Maybe there's a setting I need to change from the Interface Builder? Maybe an attribute setting the I need to disable?
The odd part is that even from the Interface Builder the fonts and colours simply never change.
Try the following code:-
#interface TestTableViewController ()<UITableViewDataSource,UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#end
#implementation TestTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.delegate=self;
self.tableView.dataSource=self;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 5;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * reuseIdentifier = nil ;
UITableViewCell* cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
}
cell.textLabel.font = [UIFont fontWithName:#"Times New Roman" size:12.0f];
cell.textLabel.textColor = [UIColor redColor];
cell.textLabel.text = [NSString stringWithFormat:#"Number: %d",indexPath.row];
return cell;
}

how can i display UITextfields inside of UITableviewcells if user click the "+" Button in UIView

I have one "+" button in UIView. My requirement is, if i click that button UITextFields displayed in UITableViewCells. I am having idea how to display the UITextFields in UIView if user clicks the "+" button. But i dont have any idea how to display UITextFields inside of UITableViewCells if user hits the "+" button. Please kindly help me anybody. Yesterday i strucked with this functionality. I tried some code. But it is not worked properly. But i had not found any solution. Thanks in advance.
UIButton *myGreenIconButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
[myGreenIconButton1 addTarget:self action:#selector(GreenIconButtonClicked)forControlEvents:UIControlEventTouchUpInside];
[myGreenIconButton1 setBackgroundImage:[UIImage imageNamed:#"index.jpg"] forState:UIControlStateNormal];
myGreenIconButton1.backgroundColor = [UIColor clearColor];
myGreenIconButton1.frame = CGRectMake(285, 144, 25, 25);
[self.view addSubview:myGreenIconButton1];
-(void)GreenIconButtonClicked
{
view=[[UIView alloc]initWithFrame:CGRectMake(0, 80, 300, 20)];
text1=[[UITextField alloc]initWithFrame:CGRectMake(10, 80, 100, 20)];
text1.borderStyle=UITextBorderStyleRoundedRect;
text1.backgroundColor=[UIColor clearColor];
text1.backgroundColor=[UIColor colorWithRed:0.662745 green:0.662745 blue:0.662745 alpha:0.5];
text1.font=[UIFont systemFontOfSize:14.0];
text1.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
text1.textAlignment=NSTextAlignmentCenter;
text1.delegate=self;
[view addSubview:text1];
text2=[[UITextField alloc]initWithFrame:CGRectMake(120, 80, 100, 20)];
text2.borderStyle=UITextBorderStyleRoundedRect;
text2.backgroundColor=[UIColor clearColor];
text2.backgroundColor=[UIColor colorWithRed:0.662745 green:0.662745 blue:0.662745 alpha:0.5];
text2.font=[UIFont systemFontOfSize:14.0];
text2.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
text2.textAlignment=NSTextAlignmentCenter;
text2.delegate=self;
[view addSubview:text2];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
[cell.textLabel setText:#""];
[view setTag:101];
NSIndexPath *ip = [NSIndexPath indexPathForRow:0 inSection:0];
UITableViewCell *cell1 = [self.tableView cellForRowAtIndexPath:ip];
UITextField *textField = (UITextField*)[cell1 viewWithTag:101];
UITextField *textField1=(UITextField*)[cell1 viewWithTag:101];
UITextField *textField2=(UITextField*)[cell1 viewWithTag:101];
[cell.contentView addSubview:text1];
[cell.contentView addSubview:text2];
[cell.contentView addSubview:text3];
return cell;
}
I have created a example for your requirement and posted it on github. You can find code Here
I have created a custom table view cell with text field on it.
First create custom cell with xib or without and do following manner its working for you :
CustomCell.h
#import <UIKit/UIKit.h>
#interface CustomCell : UITableViewCell {
}
#property (strong,nonatomic) IBOutlet UITextField *textField;
#end
CustomCell.m
#import "CustomCell.h"
#implementation CustomCell
#synthesize textField;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.textField = [[UITextField alloc] init];
[self.contentView addSubview:self.textField];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
Your TableView .h
#property (strong,nonatomic) NSMutableArray *array;
#property (strong, nonatomic) IBOutlet UITableView *tblView;
Your Table View .m
#synthesize array;
#synthesize tblView;
-(void)viewWillAppear:(BOOL)animated {
array = [[NSMutableArray alloc] init];
[tblView reloadData];
}
-(int)numberOfSectionsInTableView:(UITableView *)tableView
{
if( [array count ] > 0 )
{
return 1;
}
return 0;
}
-(int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if( [array count ] > 0 )
{
return [array Count];
}
return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = [NSString stringWithFormat:#"CustomCell%d%d",indexPath.row,indexPath.section];
[tblCreateProfile registerNib:[UINib nibWithNibName:#"CustomCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if(cell == nil)
{
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cellIdentifier =nil;
cell.textField.text = [array objectAtIndex:indexPath.row];
return cell;
}
-(void)GreenIconButtonClicked
{
[array addObject:#"Test"];
[tblView reloadData];
}
So just try in Custom TableViewCells. So that You can Achieve the solution for this problem.

Populating a TableView in ViewDidAppear

I am trying to do what I thought would be simple, but is seems quite complex.
I am trying to create a leaderboard screen.
I have the following:
NSArray* playerNames
NSArray* playerScores
My leaderboard tab is a viewcontroller. Inside, it has a tableview. The tableview has an outlet.
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#interface LeaderboardViewController : UIViewController
{
}
- (void)viewDidAppear:(BOOL)animated;
- (void) viewWillDisappear:(BOOL)animated;
#property (weak, nonatomic) IBOutlet UITableView *leaderboardTable;
- (SimonGameModel*) model;
#end
When the view did load, I get the above 2 arrays (both of same length) from my model. They correspond to the latest scores.
What I need is for each table cell to tave 2 lables so that I end up with a leaderboard that looks something like this:
Tim 200
John 100
Jack 50
etc...
I have been reading apple's docs for nearly an hour and I am confused as to how to do this.
I created a prototype with the labels I want.
Thanks
-(void)viewDidLoad {
[leaderboardTable setDataSource:self];
[leaderboardTable setDelegate:self];
}
you must create your custom cell in this way:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [playerNames count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"leader";
UITableViewCell *cell = [leaderboardTable dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UILabel *labelName = [[UILabel alloc] initWhitFrame:CGRectMake(5, 0,160,44);
[labelName setTextAlignment:NSTextAlignmentLeft];
labelName.textColor = [UIColor blackColor];
[cell.contentView addSubView:labelName];
UILabel *labelValue = [[UILabel alloc] initWhitFrame:CGRectMake(165, 0, 150, 44);
[labelValue setTextAlignment:NSTextAlignmentRight];
labelValue.textColor = [UIColor blackColor];
[cell.contentView addSubView:labelValue];
}
labelName.text = [playerNames objectAtIndex:indexPath.row];
labelValue.text = [playerScores objectAtIndex:indexPath.row];
return cell;
}
It sounds like you have not set your LeaderboardViewController as your tableview's dataSource. LeaderboardViewController will have to conform to the UITableViewDataSource protocol:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDataSource_Protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40006941
Also, remember to register a UITableViewCell xib or class with your tableview.
You will be populating your cells with data from your arrays in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

CustomCell without customCell class with storyboard

I would to create a custom cell without create a custom cell class. I know that this is possible. I've created in storyboard a tableviewcontroller with a prototype cell with a label. In the attibutes I've set the cell name "mycell". And the code that I used is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ static NSString *CellIdentifier = #"mycell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{ cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];}
UILabel *title = (UILabel*) [cell viewWithTag:1];
title.text = #"Hi";
return cell;
}
But when my app run I see only a empty table without a cell with my label.
In the tableView methods I've used:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1;}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5; }
Thank you.
If you are adding UILabel programmatically it needs to be alloc init, should be given a frame and added to the cell as sub view.
UILabel *title = (UILabel*) [cell viewWithTag:1];
title.text = #"Hi";
return cell;
Should be
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(50, 10, 50, 20)];
title.text = #"Hi";
[cell addSubview:title];
return cell;

Resources