Custom UItableViewCell label is overlapping - ios

Using UITableView and found that cells are not dequeuing properly.I have created prototype cells using storyboard,and have UIlabel over cell.
- (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];
}
UILabel*textLabel = (UILabel*)[cell viewWithTag:11];
textLabel.font=[UIFont fontWithName:#"MyUnderwood" size:16];
textLabel.text = [_tableLabelArray objectAtIndex:indexPath.row];
return cell;
}
Here is the image of UITableView which i am getting when i scroll the TableView

This could simply be a minor mistake. Try using a different tag value, or verify the values being used in the prototype cell.

You're not using your custom cell in CellForRowAtIndexPath. Code looks fine just inseatd of UITableViewCell dequeue your custom cell like so:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cell"; //needs to be the identifier you defined in story boards
YourCustomCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell=[[YourCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UILabel*textLabel = (UILabel*)[cell viewWithTag:11];
textLabel.font=[UIFont fontWithName:#"MyUnderwood" size:16];
textLabel.text = [_tableLabelArray objectAtIndex:indexPath.row];
return cell;
}
Then you need to create a subclass of UITableViewCell and name it YourCustomCell and link all IBOutlet objects like labels etc. put this line of code on top of your .h file #import YourCustomCell.h and simply access all properties of that cell like cell.textLabel instead of createting label and casting it to view with tag.

I fixed this issue by using "nil" instead of cellIdentifier
eg:
Instead of
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Use this
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:nil];
It did the trick for me

You just need to tick the "clears graphic context" property of the label in storyboard.

Related

UITableViewCell accessoryType won't be visible

The cell's accessory type won't display. I have tried both cell.accessoryType and cell.accessoryView (with an image). The table view works fine aside from this. Any suggestions? Thanks
My code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = #"Hi";
cell.detailTextLabel.text = #"Hi again!";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
Make sure your table cell width fit into the screen. You are creating UITableView programmatically or it is in nib? If you are creating programmatically, check the frame size of table view also you can share your code here.
Choose accessory type from xib / storyboard of your table view cell.

iOS 7 TableView dequeue not working

I'm having a really annoying problem that I'm not sure WHY it's happening.
I have the Table view set, I have the correct Identifier in the Cell, but it still gives me "unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard" when I run it.
the code looks like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *event = [self.events objectAtIndex:indexPath.row];
cell.textLabel.text = [event objectForKey:#"name"];
return cell;
}
Any help would be great, cause I want to rip my hair. The table is connected to that class, and then Cell has that #"Cell" Identifier
Re-check all 4 steps
STEP 1:
Your cellForRowAtIndexPath should look like this
- (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:nil];
}
NSDictionary *event = [self.events objectAtIndex:indexPath.row];
cell.textLabel.text = [event objectForKey:#"name"];
return cell;
}
STEP 2: Your "ViewController.m" File should look like this
#import "ViewController.h"
#interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
#end
STEP 3:
PLEASE CHECK - Click on view controller move to Connection inspector - See if any other unwanted connections are present in this inspector.
STEP 4: Tableview cell attribute inspector should like this]
Right, you need to register a cell for reuse. This can be done programmatically after you've created your table view like this:
static NSString *cellIdentifier = #"Cell";
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
Or this can also be done in Interface Builder by selecting the cell and navigating to the attributes inspector and changing the cell's id.
Instead of:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Try:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if that does not work after
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
add:
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
You are not checking for cell is allocate or not.. You can try below code..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
// add a placeholder cell while waiting on table data
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"cell %d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = nil;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
NSDictionary *event = [self.events objectAtIndex:indexPath.row];
cell.textLabel.text = [event objectForKey:#"name"];
}
return cell;
}
In the Document Outline, verify that your prototype UITableViewCell is a child of the UITableView.
In the Attribute Inspector, verify that your Cell Identifier and Cell Style are properly set in storyboard:
In the Identity Inspector, verify that you have the class set to UITableViewCell:

How to get UITableView Label Text string - Custom Cell

I have a UITableView with CustomCell. The custom cells contain a UILabel and a UIImageView.
I saw online that it is possible to get the text from a normal UITableView cell and store it in a string when the user presses the cell.
But how can you do this when you are using a custom cell? Becuase the name of my UILabel is "type_label" and it has been defined in the header file of my CustomCell XIB.
So in Xcode I can't use:
cell.type_label.text"
In the following function:
-(void)tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Thanks, Dan.
In the tableViewDidSelectRow all you need to do is:
UITableViewCellCustomClass *customCell = (UITableViewVellCustomClass*)[tableView cellForRowAtIndexPath:indexPath];
NSString *labelText = [[customCell type_label] text];
That should get you want you need.
Try below code - You have create type_label label's IBOutlet in CCustomCellClass
(file-->new-->subclass--> UITableViewCell-->name it like CustomCellClass)
Then implement below code
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath{
static NSString *cellIdentifier = #"Cell";
static BOOL nibsRegistered = NO;
if (!nibsRegistered) {
UINib *nib = [UINib nibWithNibName:#"CustomCellClass" bundle:nil];
[tableView registerNib:nib forCellReuseIdentifier:cellIdentifier];
}
CustomCellClass *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[CustomCellClass alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.type_label.text = #"Rocky"; // setting custom cell label
return cell;
}

Creating more detailed Table Cells

I want to create more detailed table cells. An example I found is the reddit.com client AlienBlue. Each cell has an image, the title of the post, then below it four other pieces of text. How can I "layer" text in a table cell?
Hi you should subclass UITableViewCell for custom User interface of a cell in a table view.
Also look at: UITableViewDataSource and UITableViewDelegate for other options with regard to table view.
Probably your subclassed UiTableViewCell will have custom fields for title (UILabel), caption (UILabel) and for an image (UIImageView)
Examples:
Crafting Custom UITableView Cells
Customize Table View Cells For UITableView
You will want to create a custom UITableViewCell. It's quite trivial, and can be done either entirely in code (my favorite) or through IB.
Google custom UITableView cell for some great tutorials.
Use NSMutableArray of NSMUtableDictionary, as opposed to NSArray or NSMutableArray, to hold data (strings, images, ...).
Set the table cell style property to UITableViewCellStyleSubtitle so that the table can display the detailedTextLabel (cell.detailTextLabel.text).
You can set an image to the table cell.
Example
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell...
UIImage *image = [[list1 objectAtIndex:indexPath.row] objectForKey:key1a];
cell.imageView.image = image;
NSString *categoryname = [[list1 objectAtIndex:indexPath.row] objectForKey:key1b];
cell.textLabel.text = categoryname;
cell.detailTextLabel.text = [[list1 objectAtIndex:indexPath.row] objectForKey:key1e];
cell.detailTextLabel.textColor = [UIColor blueColor];
return cell;
}
- (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];
}
cell.textLabel.text=[NSString stringWithFormat:#"%#",[appdelegate.name objectAtIndex:indexPath.row]];
UIImage *image = [[array1 objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [[array1 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