Text not being displayed in cells - ios

Trying to get my app to display text in its cells. Been scratching my head for a while. This is the code used to display the text but nothing appears. Any advice?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"leftMenuCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if (indexPath.section == 0) {
NSArray *titles = #[#"Quick Glance", #"Your Home", #"Invites"];
cell.textLabel.text = titles[indexPath.row];
}
cell.backgroundColor = [UIColor clearColor];
return cell;
}

If you have not add the following code in ViewDidLoad() method, please do so.
self.tableView.dataSource = self;
self.tableView.delegate = self;

Firstly you should break this out differently so that you are not re-creating your backing store every time you are producing a cell.
More code would be required in order to ascertain exactly what the issue but at minimum you could put the following in to verify this.
#implementation ViewController {
NSArray *_titles;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_titles = #[#"Quick Glance", #"Your Home", #"Invites"];
}
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
return [_titles count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"leftMenuCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = titles[indexPath.row];
cell.backgroundColor = [UIColor clearColor];
return cell;
}
This is the bare minimum you will need above in order to show some information in the tableview
Now the question is are you using a UITableViewController where it already has set the dataSource and delegate methods or are you using a UIViewController where the dataSource and delegate have not been set (either in code or through XIB or Storyboard)?
If you do not have the delegates set up you need to set them up either through the storyboard or through the code, and if through code and using UIViewController make sure you have a reference to the UITableView otherwise you will not be able to set the datasource.

Related

Custom Cell not working in UITableView for Search Results

At the outset I would like to tell that I have researched and tried to follow stackoverflow links such as UISearchDisplayController and custom cell but still the problem persists
I have Search Bar and Search Display controller integrated into my UITableView. The search functionality works fine but the search results cell have the default white cell design and not my custom cell design which is used for the UITableView. Below is my code to make the Search Result's Cell design adapt my custom design.
- (void)viewDidLoad
{
[super viewDidLoad];
[self.searchDisplayController.searchResultsTableView registerClass:[ItemCellTableViewCell class] forCellReuseIdentifier:#"Cell"];
if(!self.document){
[self initDocument];
}
self.filteredItemCatalogArray = [[NSMutableArray alloc]init];
self.itemCatalogTableView.dataSource = self;
self.itemCatalogTableView.delegate = self;
[self.itemCatalogTableView reloadData];
self.itemCatalogTableView.backgroundColor = [UIColor clearColor];
self.itemCatalogTableView.opaque = NO;
self.itemCatalogTableView.bounces = YES;
self.itemCatalogTableView.backgroundView = nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
if(tableView == self.searchDisplayController.searchResultsTableView)
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//cell fields customization
cell.backgroundColor = [UIColor clearColor];
cell.opaque = NO;
return cell;
}
else
{
ItemCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//cell fields customization
cell.backgroundColor = [UIColor clearColor];
cell.opaque = NO;
return cell;
}
}
What am I missing here ?
EDITED :
In the if block for search results I changed tableview to self.tableview and it gets the correct custom cell. But it takes the incorrect height which is smaller and so overlaps the rows for search results
In the if block for search results I changed tableview to self.tableview and it get the correct custom cell. But it takes the incorrect height which is smaller and so overlaps the rows for search results
to rectify the height issue I added the following line in viewdidload
self.searchDisplayController.searchResultsTableView.rowHeight =
self.tableView.rowHeight ;
1) If your cell are using a xib file you should add to viewDidLoad (or other method that will be called before tableView delegate)
[yourTableView registerNib:[UINib nibWithtName:#"your_nibName" bunde:yourBunde] forCellReuseIdentifier:#"yourIdentifier"]
also you should use registred identifier for custom cell :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
static NSString *CustomCellIdentifier = #"yourIdentifier";
if(tableView == self.searchDisplayController.searchResultsTableView)
{
// UITableViewCell customization
return cell;
}
else
{
ItemCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier forIndexPath:indexPath];
if (!cell)
cell = [[ItemCellTableViewCell alloc] initWithStyle:yourPreferedStyle reuseIdentifier:CustomCellIdentifier] // or other custom initialization
//put cell fields customization here
return cell;
}
}
If your custom cell is designed using a NIB try this
[self.searchDisplayController.searchResultsTableView registerNib:[UINib nibWithNibName:#"YourCellNibName" bundle:Nil] forCellWithReuseIdentifier:#"Cell"];
You have to get a reference of your tableView and then in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CustomTableCell";
MyCell *cell = (MyCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
...
}

Add row in UITableView in prototype cell mode (that's not what my expect)

I have UITableView with prototype cell.I have a label in cells with various values(text value).
when add a new row, some of labels in new rows created with previous cell values not default values:
What can i do?
Is my question clear?
This is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
gamers = [[NSMutableArray alloc] initWithObjects:#"Player1",#"Player2", nil];
}
- (IBAction)btnAddRow:(id)sender {
[gamers addObject:#"new player"];
[_tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *tableIdentifier = #"gamerCell";
nTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier];
if (cell == nil) {
cell = [[nTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier];
}
cell.lblTitle.text = [gamers objectAtIndex:indexPath.row];
return cell;
}
Please find the updated project in the link
UITableView-dynamic-row-buttons
Hope this helps.
Where do you update the score? if you are not doing, make, according to your logic, and then set this value in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *tableIdentifier = #"gamerCell";
nTableViewCell *cell = (nTableViewCell *)[tableView dequeueReusableCellWithIdentifier:tableIdentifier];
if (cell == nil) {
cell = [[nTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier];
}
cell.lblTitle.text = [gamers objectAtIndex:indexPath.row];
cell.lblScore.text = [NSString stringWithFormat:#"%i", (int)scoreProperty];
return cell;
}

Cannot seem to pass an array to uitableview xcode

I am trying to display the contents of an array onto a uitableview. It works fine if I assign the values individually but when I try to display them from an array xcode exits with nothing in the debugging report except (lldb). It compiles and runs great until I go to the page with the tableview. I've looked up the error and it seems to pertain to memory allocation but I am unable to manually release anything due to having to have ARC enabled for some JSON classes to operate. Can anyone take a look and see what possibly might be the issue.
- (void)viewDidLoad
{
[super viewDidLoad];
theArray = [[NSArray alloc] initWithObjects:#"1","2","3","4",nil];
// Do any additional setup after loading the view.
}
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [theArray count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Set up the cell...
self.recentSearchesTable.backgroundColor =[UIColor clearColor];
self.recentSearchesTable.separatorColor = [UIColor clearColor];
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:15];
cell.textLabel.text = [theArray objectAtIndex:indexPath.row];
cell.contentView.backgroundColor = [UIColor clearColor];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// open a alert with an OK and cancel button
NSString *alertString = [NSString stringWithFormat:#"Clicked on row #%d", [indexPath row]];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:alertString message:#"" delegate:self cancelButtonTitle:#"Done" otherButtonTitles:nil];
[alert show];
}
In viewDidLoad your array contains non-object items, change it too:
theArray = [[NSArray alloc] initWithObjects:#"1",#"2",#"3",#"4",nil];
Note the # before each item in the array, indicating that they're NSString literals (and not c strings).
You did two mistake here first you need to initialize array in this way.
theArray = [[NSArray alloc] initWithObjects:#"1",#"2",#"3",#"4",nil];
and another mistake is you need to initialize cell when it is nil
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if you want whole code then like this way you need to implement.
- (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.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
// Set up the cell...
cell.textLabel.text = [theArray objectAtIndex:indexPath.row];
cell.contentView.backgroundColor = [UIColor clearColor];
return cell;
}
First of all you do not need to disable ARC for your project due to an external class.You can exclude these classes by setting -fno-objc-arc flag.
Back to your answer
You should alloc a new cell if it can be reeused from a previous one.You should alloc you new cell if dequeueReusableCellWithIdentifier:CellIdentifier does not return a cell.
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:Cellidentifier] autorelease];
}
Apart from that when you init your datasource array in viewDidLoad you need to add # before each element to indicate NSString.

UITableView loads but nothing displayed

When I build and run there is nothing displayed. No text just empty cells.
Here is my code:
#import "plungerselection.h"
#implementation plungerselection
#synthesize tableData;
-(void) viewDidLoad
{
tableData = [[NSArray alloc] initWithObjects:#"Turbo", #"Hollow Turbo", #"Single Pad", #"Dual Pad", #"Bullet", nil];
[super viewDidLoad];
}
#pragma mark - TableView Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"MyCell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MyCell"];
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
}
return cell;
}
#end
Make sure you have wired up the datasource and delegate connections in Interface Builder.
You are only setting the text in the cell if dequeueReusableCellWithIdentifier: returns nil which in this case doesn't happen (and even if it did it will only happen a few times before all the cells that are needed get in the queue). You should set the text outside of the if (cell == nil)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"MyCell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MyCell"];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}

Static UITableViewCell backgroundView

In my project I have tableViews with static cells as well as tableViews with dynamic cells. In order to customized I've managed to get a gradient background on the cells (grouped sytle).
It works ok with dynamic TableViews as I set the background view in cellForRowAtIndex... according to the position of the row (Top, Bottom, Middle or single).
However, when I try to implement it on the static tableview cells, it doesn't work. I've tried to implement the cellForRowAtindex... but it crashes.
Does someone have an idea?
Update: the code for cellForRow..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UACellBackgroundView *bgw=[[UACellBackgroundView alloc]init];
if (indexPath.row==0) {
bgw.position = UACellBackgroundViewPositionTop;
cell.backgroundView=bgw;
}else if (indexPath.row==2){
bgw.position = UACellBackgroundViewPositionBottom;
cell.backgroundView=bgw;
}else {
bgw.position = UACellBackgroundViewPositionMiddle;
cell.backgroundView=bgw;
}
// cell.backgroundView=bgw;
return cell;
}
By the way, the Background view I got it from here: http://code.coneybeare.net/how-to-make-custom-drawn-gradient-backgrounds and here: http://pessoal.org/blog/2009/02/25/customizing-the-background-border-colors-of-a-uitableview/
if somebody is interested
It doesn't look like you are alloc the UITablViewCell, you need to alloc the cell.
For example:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// alloc the UITableViewCell
// remeber if you are not using ARC you need to autorelease this cell
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"Cell Name";
cell.detailTextLabel.text = #"Cell Detail";
return cell;
}
Add this statement:
if (cell == nil) {
// alloc the UITableViewCell
// remeber if you are not using ARC you need to autorelease this cell
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
If you have a UITableViewController subclass with a static table you should not try to dequeue cells.
Instead you should ask super for the cell. The superclass will get the cell from the storyboard and you can configure it.
Something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
UIView *selectedBackgroundView = [[UIView alloc] init];
cell.selectedBackgroundView = selectedBackgroundView;
cell.selectedBackgroundView.backgroundColor = [UIColor mb_tableViewSelectionColor];
return cell;
}
Works for all other attributes as well.

Resources