Is it possible to add a label to a UITableViewCell while reusing cells? This is the code I have been trying but when I scroll down and the cells get reused my label moves around. I just want the label in the very first row at all times, but I cannot figure this out for the life of me. I know I can not reuse the cells and it will work but I would like to reuse the cells. Please help me figure this out. Thank you very much...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
}
if ([indexPath row] == 0) {
UILabel* Label = [[UILabel alloc] initWithFrame:CGRectMake(2,2, 62, 23)];
[Label setText:#"Text"];
[cell addSubview:Label];
[Label release];
}
return cell;
}
I think I figured it out. I made the 1st row not reusable.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
static NSString *CellsToBeReused = #"CellsToBeReused";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellsToBeReused];
if (cell == nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellsToBeReused] autorelease];
}
if ([indexPath row] == 0) {
UITableViewCell *first_Cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
UILabel* Label = [[UILabel alloc] initWithFrame:CGRectMake(2,2, 62, 23)];
[Label setText:#"Text"];
Label.backgroundColor = [UIColor whiteColor];
[first_Cell.contentView addSubview:Label];
[Label release];
return first_Cell;
} else {
return cell;
}
}
Related
I want to set two background colours for single row like in this image.
The following is my code which is for array and below that there are tableView delegate methods.
-(viewDidLoad)
if([managedObject valueForKey:#"personality_company_master_values"] != nil)
{
[_displayValues addObject:[NSString stringWithFormat:#"Personality %#",[managedObject valueForKey:#"personality_company_master_values"]]];
}
if([managedObject valueForKey:#"video_tag"] != nil)
{
[_displayValues addObject:[NSString stringWithFormat:#"Tag %#",[managedObject valueForKey:#"video_tag"]]];
}
if([managedObject valueForKey:#"industry_master_values"] != nil)
{
[_displayValues addObject:[NSString stringWithFormat:#"Industry %#",[managedObject valueForKey:#"industry_master_values"]]];
}
}
Here is tableView delegate methods
- (NSArray *)myTableViewCells
{
if (!_myTableViewCells)
{
_myTableViewCells = #[
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil],
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil],
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil],
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil],
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil],
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil],
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil],
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil],
];
}
return _myTableViewCells;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self->_displayValues.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = self.myTableViewCells[indexPath.row];
NSManagedObject *managedObject = [self.devices lastObject];
UILabel *lbl=(UILabel*)[cell viewWithTag:900];
[lbl setText:[managedObject valueForKey:#"personality_company_master_values"]];
lbl.textColor=[UIColor blackColor];
[cell.contentView addSubview:lbl];
return cell;
}
Set background color for labels or try to add two view inside of your table cell content view
not enough repo to post as a comment
Add subviews, use layout constraints to size the subviews, apply the background colour to each of the views.
It's possible that you could use the labels in the cell to do that without using additional views, you'd just need to set the constraints appropriately. It actually looks like it might be a background colour on the cell and a background colour on just one of the labels...
You should have to take two Labels in subview of UITableViewCell and give two different colors to them.
Write this in cellForRowAtIndexPath method.
- (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];
for(UIView *eachView in [cell subviews])
[eachView removeFromSuperview];
//Initialize Label
UILabel *lbl1 = [[UILabel alloc]initWithFrame:YOURFRAME];
[lbl1 setFont:[UIFont fontWithName:#"FontName" size:12.0]];
[lbl1 setTextColor:[UIColor grayColor]];
lbl1.text = YOUR CELL TEXT;
[cell addSubview:lbl1];
[lbl1 release];
UILabel *lbl2 = [[UILabel alloc]initWithFrame:YOURFRAME];
[lbl2 setFont:[UIFont fontWithName:#"FontName" size:12.0]];
[lbl2 setTextColor:[UIColor blackColor]];
lbl2.text = YOUR CELL TEXT;
[cel2 addSubview:lbl2];
[lbl2 release];
return cell;
}
If you are not familiar with customisation of UITableViewCell means if you dont know how to add two different labels in the subview of UITableViewCell, then Follow this tutorial to customise your tableviewcell
Happy Coding!
plz take two label in uitableview cell and you can access them
UILabel *label =(UILabel *)[cell viewWithTag:yourtagnumber];
set this identifier value , and set the style to custom
the in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:#"youridentifier"];
NSManagedObject *managedObject = [self.devices lastObject];
UILabel *lbl=(UILabel*)[cell viewWithTag:900];
[lbl setText:[managedObject valueForKey:#"personality_company_master_values"]];
lbl.textColor=[UIColor blackColor];
return cell;
}
Hi I realize that this question has been asked before. I tried the previous solutions however to no avail unfortunately. What I am trying to do is to get my UISwitch to appear and not duplicate itself when scrolling on the table view. This is my current attempt however the UISwitch is not being displayed at all. Any help as to what I am doing wrong would be greatly appreciated!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"RestaurantCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
Restaurant *restaurant = [restaurants objectAtIndex:indexPath.row];
UISwitch *notificationSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(245, 15, 79, 27)];
[notificationSwitch addTarget:self action:#selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
[cell.contentView addSubview:notificationSwitch];
cell.textLabel.text = restaurant.name;
cell.detailTextLabel.text = restaurant.hours;
return cell;
}
Try this:
UISwitch init Method what say
- (id)initWithFrame:(CGRect)frame
Parameters
frame
A rectangle defining the frame of the UISwitch object. The size components of this rectangle are ignored.
Discussion
UISwitch overrides initWithFrame: and enforces a size appropriate for the control
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"RestaurantCell";
UISwitch *notificationSwitch = nil;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
notificationSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
[notificationSwitch addTarget:self action:#selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
notificationSwitch.tag = 101;
cell.accessoryView = switchView;
}
if(!notificationSwitch) {
notificationSwitch = (UISwitch*)[cell.accessoryView viewWithTag:101];
}
Restaurant *restaurant = [restaurants objectAtIndex:indexPath.row];
switch (indexPath.section) {
case 0:
cell.textLabel.text = restaurant.name;
cell.detailTextLabel.text = restaurant.hours;
break;
default:
cell.textLabel.text = #"oh no!";
}
return cell;
}
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'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];
}
}
I'm having a bit of difficulty understanding what is going wrong and how to fix this.
-(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];
NSUInteger row = [indexPath row];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
UILabel *where = [[UILabel alloc] initWithFrame:CGRectMake(88.0, 0.0, 155.0, 22.0)];
where.text = [delegate.destinationArray1 objectAtIndex:row];
where.font = [UIFont fontWithName:#"Helvetica" size:12.0];
where.textColor = [UIColor blueColor];
[cell.contentView addSubview:where];
return cell;
}
This doesn't work properly but this does:
-(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];
}
NSUInteger row = [indexPath row];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
UILabel *where = [[UILabel alloc] initWithFrame:CGRectMake(88.0, 0.0, 155.0, 22.0)];
where.text = [delegate.destinationArray1 objectAtIndex:row];
where.font = [UIFont fontWithName:#"Helvetica" size:12.0];
where.textColor = [UIColor blueColor];
[cell.contentView addSubview:where];
return cell;
}
They both get populated by "delegate.destinationArray1" but when all the code is inside the curly braces of
if(cell == nil)
the list gets unordered and repeats cells and misses some out. I can't use the latter way as it creates a MASSIVE memory leak when scrolling.
Any ideas?
I did the exact same thing when I started using UITableViews. The reason for the memory leak is that in the second implementation (the one that works) you are actually creating every cell, every single time. Let me try to explain a bit more.
You never want to set content of a cell between the (cell == nil). The reason for this is the reuseIdentifier. If the table needs to display a new cell it will grab one and see if it has already been alloced/inited. If it has it will just use it. If that is the case the content will already be set in the cell you grabbed and you are not telling it to set it any differently.
between the (cell == nil) only create and establish the view. Not the content. All content should be set after. So then no matter what cell it grabs it can always set the content. So this is what you want:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell) // or (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
UILabel *where = [[UILabel alloc] initWithFrame:CGRectMake(88.0, 0.0, 155.0, 22.0)];
where.font = [UIFont fontWithName:#"Helvetica" size:12.0];
where.textColor = [UIColor blueColor];
where.tag = 1;
[cell addSubview:where];
}
NSUInteger row = indexPath.row;
UILabel *where = [cell viewWithTag:1];
where.text = [delegate.destinationArray1 objectAtIndex:row];
return cell;
}
I just coded this in StackoverFlow so sorry if there are any small syntax errors.
The cell object is reused or created by the first statement. After checking cell for nil and creating a cell, you must not create another cell.
So delete the line
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
which comes after
NSUInteger row = [indexPath row];
and you'll work with the correct cell object.
When the tableview reuses cells it is based on the CellIdentifier. The tableview doesn't care what attributes you've set on the cell. In the first case the reuse it happening and it recognizes a cell it can use but that cell has the wrong information on it.
What I do is subclass UITableViewCell and do all the work inside of that class. Here is a quick snippet
#implementation AlertCell
//Custom init method
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier withHeight:(float)height {
//Whatever you need to do
}
//Place the views
- (void)layoutSubviews {
}
//Custom Setter method
- (void)setAlert:(CWAlert *)incomingAlert withAssets:(NSDictionary *)assets {
}
#end
Then you do something like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier;
CWAlertCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[AlertCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier withHeight:[self convertAssetsLengthToCellHeight:assetsLength]];
UIView *selectedView = [[UIView alloc] init];
selectedView.backgroundColor = [UIColor colorWithHexString:#"F6F6F6"];
cell.selectedBackgroundView = selectedView;
}
NSDictionary *alertInfo = [AlertCell getNeededCellAssets:alert];
[cell setAlert:alert withAssets:alertInfo];
return cell;
}
I can show more code from the subclass if needed.