setIndentationLevel is not working for cell.ContentView subviews - iphone

Why setIndentationLevel is not getting with cells subviews?
I have a UITableView. My cellForRowAtIndexPath is like below. The arrow which i added is not shifting with the textlabel. why?
How can I do this?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"small_arrow.png"]];
img.frame = CGRectMake(5, 10, 7, 11);
[cell.contentView addSubview:img];
cell.textLabel.text=[[self.tabledata objectAtIndex:indexPath.row] valueForKey:#"name"];
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:18];
[cell setIndentationLevel:[[[self.tabledata objectAtIndex:indexPath.row] valueForKey:#"level"] intValue]];
return cell;
}

Yeah !!! Its workin now.
I created a custom cell and its m file is like
#import "customecell.h"
#implementation customecell
#synthesize textLbl,img;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
textLbl = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, 320, 20)];
textLbl.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:textLbl];
img = [[UIImageView alloc]initWithFrame:CGRectMake(5, 10, 7, 11)];
img.image = [UIImage imageNamed:#"small_arrow.png"];
[self.contentView addSubview:img];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)layoutSubviews{
[super layoutSubviews];
float indentPoints = self.indentationLevel * self.indentationWidth;
self.contentView.frame = CGRectMake(
indentPoints,
self.contentView.frame.origin.y,
self.contentView.frame.size.width - indentPoints,
self.contentView.frame.size.height
);
}
#end
and the cellForRowAtIndexPath is changed to
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
customecell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[customecell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLbl.text=[[self.tabledata objectAtIndex:indexPath.row] valueForKey:#"name"];
cell.textLbl.font = [UIFont fontWithName:#"Helvetica" size:18];
[cell setIndentationLevel:[[[self.tabledata objectAtIndex:indexPath.row] valueForKey:#"level"] intValue]];
cell.indentationWidth = 25;
float indentPoints = cell.indentationLevel * cell.indentationWidth;
cell.contentView.frame = CGRectMake(indentPoints,cell.contentView.frame.origin.y,cell.contentView.frame.size.width - indentPoints,cell.contentView.frame.size.height);
return cell;
}

Related

Load more at the top of uitableview

I need a load more cell at the top of my table populated with custom cell, the first time that I populate the table is all ok, no overlap and no problem, if I try to load more cell this cell appear but with the wrong height and if I try to scroll the cell change position randomly!
This is my code, where the mistake for you?
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.arrayMessaggi count] + 1;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (indexPath.row == 0) {
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UILabel *labelLoad = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[labelLoad setBackgroundColor:[UIColor clearColor]];
[labelLoad setTextAlignment:NSTextAlignmentCenter];
[labelLoad setText:[NSString stringWithFormat:#"%d - Load more...",indexPath.row]];
[labelLoad setText:#"Load more..."];
[labelLoad setTag:3];
[cell.contentView addSubview:labelLoad];
}
UILabel *labelLoad = (UILabel *)[cell.contentView viewWithTag:3];
[labelLoad setText:[NSString stringWithFormat:#"%d - Load more...",indexPath.row]];
}
else {
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
PFObject *obj = [self.arrayMessaggi objectAtIndex:indexPath.row - 1];
PFUser *user = [obj objectForKey:#"daUtente"];
float height = [self getStringHeight:[obj objectForKey:#"TestoMessaggio"] andFont:[UIFont systemFontOfSize:13]];
UILabel *labelUser = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 280, 20)];
[labelUser setBackgroundColor:[UIColor clearColor]];
[labelUser setText:[user objectForKey:#"username"]];
[labelUser setFont:[UIFont systemFontOfSize:13]];
[labelUser setTag:1];
[cell.contentView addSubview:labelUser];
}
PFObject *obj = [self.arrayMessaggi objectAtIndex:indexPath.row - 1];
PFUser *user = [obj objectForKey:#"daUtente"];
UILabel *labelUser = (UILabel *)[cell.contentView viewWithTag:1];
[labelUser setText:[NSString stringWithFormat:#"%d - %#",indexPath.row,[user objectForKey:#"username"]]];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
float height = 44;
if (indexPath.row > 0) {
PFObject *obj = [self.arrayMessaggi objectAtIndex:indexPath.row - 1];
NSString *text = [obj objectForKey:#"TestoMessaggio"];
height = 30 + [self getStringHeight:text andFont:[UIFont systemFontOfSize:13]] + 10;
}
return height;
}
- (void) setPullDownToRefresh {
refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:#selector(aggiornaTabella:) forControlEvents:UIControlEventValueChanged];
[self.messagesTableView addSubview:refreshControl];
}
- (void) aggiornaTabella:(UIRefreshControl *)myRefreshControl {
self.arrayMessaggi = [NSMutableArray arrayWithArray:objects];
self.messagesTableView = [[UITableView alloc] init];
[self.messagesTableView reload data];
}

UITableViewCell iOS6

I have project(AppDelegate, ViewController and UITableViewCell).
//ViewController.m(Excerpt)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TableCell *cell = (TableCell *)[myTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
//====A====
cell = [[TableCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
//====B==== //cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UIView *bgView = [[UILabel alloc] initWithFrame:CGRectZero];
cell.backgroundView = bgView;
for(UIView *view in cell.contentView.subviews){
view.backgroundColor = [UIColor clearColor];
}
}
cell.titleLabel.text = [NSString stringWithFormat:#"%# %i", #"row", indexPath.row];
if(indexPath.row % 2){
cell.backgroundView.backgroundColor = [UIColor whiteColor];
}else{
cell.backgroundView.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:0.1];
}
return cell;
}
//TableCell.m(UITableViewCell class)
#import "TableCell.h"
NSString *CellIdentifier = #"CellIdentifier";
#implementation TableCell
#synthesize titleLabel;
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier])
{
titleLabel = [[UILabel alloc] initWithFrame:frame];
titleLabel.font = [UIFont systemFontOfSize:15];
titleLabel.frame = CGRectMake(10.0, 0.0, 320.0, 40.0);
[self.contentView addSubview:titleLabel];
self.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
#end
warning(initWithFrame:...iOS3) is generated in the part //===A===. Therefore I write //===B===. Nothing in cell.
I want to get rid warning.
initWithFrame:reuseIdentifier: is a deprecated method since iOS 3.0 (lot of time). Use the newer initWithStyle:reuseIdentifier: and the warning will go away.
UITableViewCell documentation

UIImageView in UITableViewCell not showing up until cell is being reused

I am having a weird issue with a custom UITableViewCell implementation that I have:
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
ProfileTableViewCell *cell = (ProfileTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[ProfileTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
MyStory *myStory = (SavedStory *)[fetchedResultsController.fetchedObjects objectAtIndex:indexPath.row];
[cell setupCellViewWithSavedStory:myStory];
return cell;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithPulseStyle:PNRTableViewCellAccessoryDefault reuseIdentifier:#"SavedStories"];
if (self) {
storyImageView_ = [[UIImageView alloc] initWithFrame:CGRectMake(kPadding, kPadding, kImageSize, kImageSize)];
storyImageView_.layer.borderWidth = 2.0;
storyImageView_.layer.borderColor = [UIColor whiteColor].CGColor;
storyTitleLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(storyImageView_.frameRight + kPadding*1.5, storyImageView_.frameY, self.bounds.size.width - storyImageView_.frameWidth - 4*kPadding, 0)];
storyTitleLabel_.numberOfLines = 0;
storyTitleLabel_.lineBreakMode = UILineBreakModeClip;
storyTitleLabel_.font = [UIFont fontWithName:kProximaNovaBold size:14];
storyTitleLabel_.textColor = [UIColor whiteColor];
storyTitleLabel_.backgroundColor = [UIColor clearColor];
storyPublisherLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(storyTitleLabel_.frameX, storyTitleLabel_.frameHeight, storyTitleLabel_.frameWidth, 50)];
storyPublisherLabel_.numberOfLines = 1;
storyPublisherLabel_.font = [UIFont fontWithName:kProximaNova size:11];
storyPublisherLabel_.textColor = [UIColor colorWithWhite:140/255.f alpha:1.0];
storyPublisherLabel_.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:storyImageView_];
[self.contentView addSubview:storyTitleLabel_];
[self.contentView addSubview:storyPublisherLabel_];
}
return self;
}
-(void)setupCellViewWithSavedStory:(MyStory *) myStory
{
if (myStory.imageState == StoryImageAvailableOnDisk) {
storyImageView_.hidden = NO;
storyImageView_.image = myStory.image;
}
else {
storyImageView_.image = nil;
storyImageView_.hidden = YES;
}
CGSize titleSize = [myStory.title sizeWithFont:storyTitleLabel_.font constrainedToSize:CGSizeMake(storyTitleLabel_.frameWidth, kImageSize - kPadding) lineBreakMode:storyTitleLabel_.lineBreakMode];
[storyTitleLabel_ setFrameHeight:titleSize.height];
[storyTitleLabel_ setText:savedStory.title];
[storyPublisherLabel_ setText:savedStory.domain];
[self setNeedsLayout];
}
But for some reason is that I can't see image until I scroll down and scroll back up again. In other words the image is reused then I can see the image. All of the label text is showing just fine, it's just the image. Any idea why?
Try adding this code:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
MyStory *myStory = (SavedStory *)[fetchedResultsController.fetchedObjects objectAtIndex:indexPath.row];
[cell setupCellViewWithSavedStory:myStory];
}
Have you tried this?
if (cell == nil) {
cell = [[ProfileTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
MyStory *myStory = (SavedStory *)[fetchedResultsController.fetchedObjects objectAtIndex:indexPath.row];
[cell setupCellViewWithSavedStory:myStory];
}

UITableViewCell row disorder

I posted some question with same source code before. I just found out some other strange thing.The thing is that If I define reuse cell identifier, each row color is weird.
But If I don't use reuse identifier, its working.
Please give me any tips why each row color does not keep the order.
//its working
static NSString *CellIdentifier = #"Cell";
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"] autorelease]
cell.contentView.backgroundColor = (indexPath.row %2) ? [UIColor redColor] : [UIColor yellowColor];
//it does not work.
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; -- does not working.
cell.contentView.backgroundColor = (indexPath.row %2) ? [UIColor redColor] : [UIColor yellowColor];
- (void)viewDidLoad {
[super viewDidLoad];
//Initialize the array.
listOfItems = [[NSMutableArray alloc] init];
//Add items
[listOfItems addObject:#"1"];
[listOfItems addObject:#"2"];
[listOfItems addObject:#"3"];
[listOfItems addObject:#"4"];
[listOfItems addObject:#"5"];
[listOfItems addObject:#"6"];
[listOfItems addObject:#"7"];
[listOfItems addObject:#"8"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
#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 [listOfItems count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UILabel *aLabel; UILabel *bLabel; UILabel *v1Label; UILabel *v2Label;; UIView *v1; UIView *v2;
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSLog(#" cell null");
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.contentView.backgroundColor = (indexPath.row %2) ? [UIColor redColor] : [UIColor yellowColor];
aLabel = [[[UILabel alloc] initWithFrame:CGRectMake(9.0, 8.0, 50.0, 20.0)] autorelease];
aLabel.tag = 1;
aLabel.font = [UIFont systemFontOfSize:30];
[cell.contentView addSubview:aLabel];
v1 = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 116)];
v1.backgroundColor = [UIColor whiteColor];
v1.tag = 10;
v1.hidden = YES;
[cell.contentView addSubview:v1];
[v1 release];
UILabel *a = [[[UILabel alloc] initWithFrame:CGRectMake(0, 10, 100, 100)] autorelease];
a.text = #"v1.test1";
[v1 addSubview:a];
UILabel *b = [[[UILabel alloc] initWithFrame:CGRectMake(0, 40, 100, 100)] autorelease];
b.text = #"v1.test2";
[v1 addSubview:b];
v2 = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 100)];
v2.backgroundColor = [UIColor blueColor];
v2.tag = 11;
v2.hidden = YES;
[cell.contentView addSubview:v2];
[v2 release];
UILabel *c = [[[UILabel alloc] initWithFrame:CGRectMake(0, 10, 100, 100)] autorelease];
c.text = #"v2.test1";
[v2 addSubview:c];
UILabel *d = [[[UILabel alloc] initWithFrame:CGRectMake(0, 40, 100, 100)] autorelease];
d.text = #"v2.test2";
[v2 addSubview:d];
}
else {
aLabel = (UILabel *)[cell.contentView viewWithTag:1];
//
v1 = (UIView *) [cell.contentView viewWithTag:10];
v2 = (UIView *) [cell.contentView viewWithTag:11];
}
aLabel.text = [listOfItems objectAtIndex:indexPath.row];
if (SelectedIndexPath == indexPath.row)
{
if ([aLabel.text intValue] % 2) {
v1.hidden = NO;
v2.hidden = YES;
}
else {
v1.hidden = YES;
v2.hidden = NO;
}
}
else {
v1.hidden = YES;
v2.hidden = YES;
}
return cell;
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (SelectedIndexPath == indexPath.row)
{
return 162.0;
}
else {
return 46.0;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (SelectedIndexPath == -1)
{
OldSelectedIndexPath = indexPath.row;
SelectedIndexPath = indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:NO];
}
else
{
if (SelectedIndexPath == indexPath.row)
{
OldSelectedIndexPath = SelectedIndexPath;
SelectedIndexPath = -1;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:NO];
}
else
{
SelectedIndexPath = indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:OldSelectedIndexPath inSection:indexPath.section], indexPath, nil] withRowAnimation:NO];
OldSelectedIndexPath = SelectedIndexPath;
}
}
}
Move the coloring code outside of the if (cell == nil) { block. Just because a cell was created for an even-numbered index doesn't mean it will only be reused for even ones.
Here's a simple example of code for coloring cells that are being reused:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20;
}
- (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];
}
cell.textLabel.text = #"Anything";
cell.contentView.backgroundColor = (indexPath.row %2) ? [UIColor redColor] : [UIColor yellowColor];
return cell;
}

How to pass a value from the TableView controller to a customCell class

I'm creating a custom UITableViewCell programmatically, which uses the following code. What im wondering is how to make the background of it Transparent?
Reason why I want it transparent is that the UITableViewCell's height is dynamic and i cant figure out how to pass the "dynamic height" code to the customCell class, so i figured i'd set a max height of 300 or so for the CGRect and make the background transparent so it doesn't overlap the following cells.
Making the label transparent doesn't work, so I need to pass the TableViewCell height to the custom class.
customCell class
#import "customCell.h"
#implementation customCell
#synthesize primaryLabel;
#synthesize secondaryLabel;
#synthesize priceLabel;
#synthesize rectColor;
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
if (self == [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {
// Initialization code
primaryLabel = [[UILabel alloc]init];
primaryLabel.textAlignment = UITextAlignmentLeft;
primaryLabel.font = [UIFont systemFontOfSize:10];
secondaryLabel = [[UILabel alloc]init];
secondaryLabel.textAlignment = UITextAlignmentLeft;
secondaryLabel.font = [UIFont systemFontOfSize:8];
priceLabel = [[UILabel alloc]init];
priceLabel.textAlignment = UITextAlignmentLeft;
priceLabel.font = [UIFont systemFontOfSize:10];
//self.rectColor = kDefaultRectColor;
[self.contentView addSubview:primaryLabel];
[self.contentView addSubview:secondaryLabel];
[self.contentView addSubview:priceLabel];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGRect frame;
//CGContextSetFillColor(context, [UIColor clearColor]);
frame= CGRectMake(boundsX+5 ,5, 200, 15);
primaryLabel.frame = frame;
frame= CGRectMake(boundsX+10 ,20, 300, 15);
secondaryLabel.frame = frame;
frame= CGRectMake(boundsX+270 ,0, 50, 15);
priceLabel.frame = frame;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)dealloc
{
[super dealloc];
}
#end
UITableView Class
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
customCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
//cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell = [[[customCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.primaryLabel.text = [[data objectAtIndex:indexPath.row] objectForKey:#"title"];
cell.primaryLabel.font = [UIFont boldSystemFontOfSize:18];
cell.primaryLabel.numberOfLines = ceilf([[[data objectAtIndex:indexPath.row] objectForKey:#"description"] sizeWithFont:[UIFont boldSystemFontOfSize:18] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap].height/20.0);
cell.secondaryLabel.text = [[data objectAtIndex:indexPath.row] objectForKey:#"description"];
cell.secondaryLabel.font = [UIFont systemFontOfSize:14];
cell.secondaryLabel.numberOfLines = ceilf([[[data objectAtIndex:indexPath.row] objectForKey:#"description"] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap].height/20.0);
cell.priceLabel.text = [[data objectAtIndex:indexPath.row] objectForKey:#"price"];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *titleString = [[data objectAtIndex:indexPath.row] objectForKey:#"title"];
NSString *detailString = [[data objectAtIndex:indexPath.row] objectForKey:#"description"];
CGSize titleSize = [titleString sizeWithFont:[UIFont boldSystemFontOfSize:18] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
CGSize detailSize = [detailString sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
return detailSize.height+titleSize.height;
}
If you make your table cells massive and transparent you're going to make the rendering fantastically slow!
I'd take a look at the delegate protocol - this method is how you tell a cell how high it should be.
Calling [tabelView reloadData] will cause this method to be called for each cell. Put your dynamic height code in there.
Hope that helps.
set the backgroundColor on the cell to be "[UIColor clearColor]"
Because your row height is dependent on your custom cell layout (based on labels), I would put the sizing logic inside the custom cell class (in layoutSubviews) and in your tableViewController do the following:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// initialize and configure your cell
[cell setNeedsLayout];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
return cell.bounds.size.height;
}
And I agree with the others that transparent cells should be avoided if possible.