this is my problem. 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.
Your UILabel title is nil. First create a UILabel and add as a subview to your cell. You can do this like below
- (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];
if (title == nil) {
title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 15)];
title.tag = 1;
[cell.contentView addSubview:title];
}
title.text = #"Hi";
return cell;
}
Related
This is my code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];}
self.credits = (UILabel*)[cell viewWithTag:101];
self.credits.text = #"Available Credits";
cell.backgroundColor = [UIColor redColor];
self.credits.textColor = [UIColor blackColor];
[cell.contentView addSubview:self.credits];
credits is the label here
You store a cell label in your UIViewController and then try to addSubview in the content view.
The right approach for this should be:
Create a subclass of UITableViewCell
Assign a label inside this cell as a property (could be by code, by xib)
In cellForRowAtIndexPath: use that property name to assign Available Credits
Your cellForRowAtIndexPath should look like this in the end:
- (MyCustomUITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCustomUITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
self.myNewProperty.text = #"Available Credits";
}
I use bellow codes to show data on TableView but when scrolling, data repeat and other data lost.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [FileCompletedArray count];
}
cellForRowatindexpath ():
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UILabel *FileNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
FileNameLabel.backgroundColor = [UIColor clearColor];
FileNameLabel.font = [UIFont fontWithName:#"Helvetica" size:16];
FileNameLabel.font = [UIFont boldSystemFontOfSize:16];
FileNameLabel.textColor = [UIColor blackColor];
NSLog(#"Reseversed TEMP array %#",FileCompletedArray);
FileNameLabel.text =[FileCompletedArray objectAtIndex:indexPath.row];
[cell.contentView addSubview: FileNameLabel];
[FileNameLabel release];
}
return cell;
}
Do you have any solutions? Thanks in advance
use cell Identifier different
For example like bellow...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"%d,%d",indexPath.section,indexPath.row];
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
/// write your code here..
}
}
OR set nil like bellow..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
/// write your code here..
}
}
This problem is generated because Your UITableView Reuse Cell instant of create new one.
I give you some suggestion that may be helpful for you.
1) add Controller in between
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
/// Your code of initialization of controller;
}
/// set property of controllers such like (Text UILabel, image of UIImageView...etc) .
return cell;
}
1) Add Your Controller to cell.contentView.
EDITED:
Before You follow my Edited Answer i want to tell you that following code is bad for memory management because it will create new cell for each rows of UITableView, so be careful for it.
But it is better for if UITableView Have Limited row about (50-100 may be ) os use following code if is it suitable for you.
NSString *CellIdentifier = [NSString stringWithFormat:#"S%1dR%1d",indexPath.section,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
/// Your whole code of controllers;
}
You are setting the text of the label only when creating a new cell. However, during cell reuse, new cell is not created. Hence your code for setting/changing text is not working. You can use this modified code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *FileNameLabel=nil;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
FileNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
FileNameLabel.tag = 1001;
FileNameLabel.backgroundColor = [UIColor clearColor];
FileNameLabel.font = [UIFont fontWithName:#"Helvetica" size:16];
FileNameLabel.font = [UIFont boldSystemFontOfSize:16];
FileNameLabel.textColor = [UIColor blackColor];
[cell.contentView addSubview: FileNameLabel];
[FileNameLabel release];
}
if(!FileNameLabel)
FileNameLabel = [cell.contentView viewWithTag:1001];
FileNameLabel.text =[FileCompletedArray objectAtIndex:indexPath.row];
return cell;
}
Alternatively you can use the default textLabel instead of creating and adding new label
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *FileNameLabel=nil;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:16];
cell.textLabel.font = [UIFont boldSystemFontOfSize:16];
cell.textLabel.textColor = [UIColor blackColor];
}
cell.textLabel.text =[FileCompletedArray objectAtIndex:indexPath.row];
return cell;
}
I also faced same problem, the problem occurs when we reuse the cells. At the time of reusing the data is already present in it and we write some more data also, to sort out this, i did a minor change to my table view delegate method cellForRowAtIndexPath. Here the code which i use always using table view.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
}
for (UIView *view in cell.contentView.subviews) {
[view removeFromSuperview];
}
return cell;
}
The for loop gets executed when we are reusing some cell and what it does is removing all the previous data present in UITableViewCell
Hope this will help someone.
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;
I'm adding two custom UILabel to a section of my UITableView in this way:
//in .h file:
NSArray *listaopzioni;
#property (nonatomic, retain) NSArray *listaopzioni;
//in .m file:
self.listaopzioni = [[NSArray arrayWithObjects:#"Strumenti",#"Help & Credits", nil] retain];
- (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] autorelease];
}
if ([indexPath section]==0) {
cell.accessoryType = UITableViewCellAccessoryNone;
UILabel *slogan= [[UILabel alloc] initWithFrame:CGRectMake(0,0,cell.frame.size.width,cell.frame.size.height)];
slogan.text=[listaopzioni objectAtIndex:indexPath.row];
slogan.textAlignment=UITextAlignmentCenter;
slogan.font= [UIFont boldSystemFontOfSize:20];
slogan.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:slogan];
[slogan release];
}
}
All woks perfectly, but when I slide up and down the tableview (trying to cover the cells below the UINavigationBar) I get a strange effect: the text is overlapped just making each letter thicker.
What's wrong?
Method cellForRowAtIndexPath is called everytime the cell becomes visible.
That is why it creates labels everytime you scroll.
The solution is to put Label creation when you create the cell:
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
if ([indexPath section]==0) {
cell.accessoryType = UITableViewCellAccessoryNone;
UILabel *slogan= [[UILabel alloc] initWithFrame:CGRectMake(0,0,cell.frame.size.width,cell.frame.size.height)];
slogan.text=[listaopzioni objectAtIndex:indexPath.row];
slogan.textAlignment=UITextAlignmentCenter;
slogan.font= [UIFont boldSystemFontOfSize:20];
slogan.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:slogan];
[slogan release];
}
}
UITableViewCells (when used properly) get reused, which means after they've been created, they keep their created state, i.e. the label has been added to your cell. What you need to do is make use of this cell reuse to your advantage:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel slogan;
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
slogan = [[UILabel alloc] initWithFrame:CGRectMake(0,0,cell.frame.size.width,cell.frame.size.height)];
slogan.tag = 2121; // Any unique-to-the-cell, positive integer
slogan.textAlignment=UITextAlignmentCenter;
slogan.font= [UIFont boldSystemFontOfSize:20];
slogan.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:slogan];
} else {
slogan = [cell viewWithTag:2121]; // Must match slogan.tag
}
if ([indexPath section]==0) {
cell.accessoryType = UITableViewCellAccessoryNone;
slogan.text=[listaopzioni objectAtIndex:indexPath.row];
}
}
So far, I used to create custom nibs to make my cell as I wanted but this time, the height of a cell will change from one to another so that I can't create a fixed-size cell's nib.
So I decided to create it programmatically ... Is the way below the good way to achieve it ?
// Customize the appearance of table view cells.
- (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];
UILabel *pseudoAndDate = [[UILabel alloc] initWithFrame:CGRectMake(0.0,0.0,320.0,20.0)];
[pseudoAndDate setTag:1];
[cell addSubview:pseudoAndDate];
[pseudoAndDate release];
}
CommentRecord *thisRecord = [comments objectAtIndex:[indexPath row]];
UILabel *label = (UILabel *)[cell viewWithTag:1];
[label setText:[NSString stringWithFormat:#"%# | %#",thisRecord.author,thisRecord.date]];
return cell;
}
or .. am i missing something here ? Cause so far it doesn't seem to work ;)
Thanks,
Gotye.
Why create a label when you don't need to? Use the UITableViewCell's label.
- (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];
}
CommentRecord *thisRecord = [comments objectAtIndex:[indexPath row]];
cell.textLabel.text = [NSString stringWithFormat:#"%# | %#",thisRecord.author,thisRecord.date];
return cell;
}
If your problem is that the height varies from cell to cell you can use the method:
https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:heightForRowAtIndexPath:
From UITableViewDelagate to achieve it
New link for custom UITableViewCell programmatically Apple Documentation UITableViewCell