I created mycell,and in myTableview I created a
#property (strong ,nonatomic)NSArray *contentPictureArray;
this is:
self.contentPictureArray = #[[UIImage imageNamed:#"wallha#2x.png"],#"wallha#2x.png"],[UIImage imageNamed:#"wallha#2x.png"]];
I want to use cell.contentPictureView.view=[self.contentPictureArray objectAtIndex:indexPath.row]; but not true ,how can I add mycell to the cellForRowAtIndexPath?
cell.stateView = [[UIView alloc] initWithFrame:CGRectMake(XB, HeiB+NY, WidB, 0)];
[cell addSubview:cell.stateView];
cell.contentTextView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WidB, 0)];
[cell.stateView addSubview:cell.contentTextView];//动态文字部分
cell.contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WidB, 0)];
[cell.contentTextView addSubview:cell.contentLabel];//动态文字label
cell.allContentBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, cell.contentLabel.frame.size.height, HeiB, 2*NX)];
[cell.contentTextView addSubview:cell.allContentBtn];//全文显示按钮
cell.contentPictureView = [[UIView alloc] initWithFrame:CGRectMake(0, cell.stateView.frame.origin.y+2*NX, HeiB+NY, 0)];
[cell.stateView addSubview:cell.contentPictureView];
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
KBTableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil) {
cell = [[KBTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdent:#"cell"];
}
if(indexPath.row==0||indexPath.row==1){
cell.iconImageView.image = [UIImage imageNamed:#"avatar.png"];
}
cell.contentLabel.text=[self.listArray objectAtIndex:indexPath.row];
cell.nameLabel.text = [self.nameArray objectAtIndex:indexPath.row];
cell.timeLabel.text =[self.timeArray objectAtIndex:indexPath.row];
cell.contentPictureView.=[self.contentPictureArray objectAtIndex:indexPath.row];
return cell;
}
I don't understand.
In your code:
[self.contentPictureArray objectAtIndex:indexPath.row] is UIImage
cell.contentPictureView.view is UIView ?
so you code:
cell.contentPictureView.view = [self.contentPictureArray objectAtIndex:indexPath.row]
how you can do it?
Add the image like this in your cellForRowAtIndexPath
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 10,100, 200)];
imageView.backgroundColor = [UIColor clearColor];
imageView.image = [contentPictureArray objectAtIndex:indexPath.row] ;
[cell.contentView addSubview:imageView];
Hope this helps...
You can initialize array with image name only like below.
self.contentPictureArray = #[#"wallha#2x","wallha#2x",#"wallha#2x"]];
Now use below code:
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 10,100, 100)];
imgView.backgroundColor = [UIColor clearColor];
imgView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[contentPictureArray objectAtIndex:indexPath.row] ofType:#"png"]] ;
[cell.contentView addSubview:imageView];
Also if you want you can create a custom cell also.
Related
hi im creating chat example and if user add send button adding new object in my arrray and reload data but its overwrite old table.
its my array :
arr1 = [[NSMutableArray alloc] initWithObjects:#"Whats up today ? ", #"Hello.", nil];
Here my source :
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simleTableIdentifier = #"TeamCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"TeamCell"];
}
cell.backgroundColor = [UIColor clearColor];
tableView.scrollEnabled = YES;
UIImageView *myRect = [[UIImageView alloc] initWithFrame:CGRectMake(20, 30, 10, 12)];
myRect.image = [UIImage imageNamed:#"tri.png"];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"jj.jpg"]];
UILabel *label = [[UILabel alloc] init];
label.text = [arr1 objectAtIndex:indexPath.row];
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByWordWrapping;
label.font =[UIFont systemFontOfSize:14.0];
label.text = [arr1 objectAtIndex: indexPath.row];
label.textColor = [UIColor blackColor];
label.backgroundColor = [UIColor clearColor];
label.frame = CGRectMake(40, 40, 210, 80);
[label sizeToFit];
UIImageView *imgim = [[UIImageView alloc] initWithFrame:CGRectMake(30, 10, label.frame.size.width+20, label.frame.size.height+20)];
imgim.image = [UIImage imageNamed:#"bg.png"];
CGRect myRectANGLE = CGRectMake(imgim.frame.origin.x+10,imgim.frame.origin.y+10, label.frame.size.width, label.frame.size.height);
[label setFrame:myRectANGLE];
CALayer *addRadius = [imgim layer];
[addRadius setMasksToBounds:YES];
[addRadius setCornerRadius:5.0];
[self.MyTableView setSeparatorColor:[UIColor clearColor]];
[self.MyTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
self.MyTableView.transform = CGAffineTransformMakeScale(1, -1); //in viewDidLoad
cell.transform = CGAffineTransformMakeScale(1, -1);//in tableView:cellForRowAtIndexPath:
[[cell contentView] addSubview:myRect];
[[cell contentView] addSubview:imgim];
[[cell contentView] addSubview:label];
return cell;
}
İf click send button i reload data but its overwite on old-data look like my photo :
-(IBAction)btnTap:(UIButton *)sender {
[arr1 insertObject:Textify.text atIndex:0];
NSLog(#"%i", arr1.count);
[MyTableView reloadData];
}
Try clearing the cell's contentView subview, and then add the new subviews.
Here's a block of code for removing the subviews, taken from this post.
if ([cell.contentView subviews]){
for (UIView *subview in [cell.contentView subviews]) {
[subview removeFromSuperview];
}
}
UITableViewCell will be reused while reload, you should not create subviews again to add on cell.contentView.Try to use code below
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"TeamCell"];
UILabel *label = [[UILabel alloc] initWithFrame:someFrame];
label.tag = 1000;
[cell.contentView addSubview:label];
}
UILabel *label = [cell.contentView viewWithTag:1000];
label.text = #"something";
I'm not sure what I'm missing but I'm trying to display a new viewController and passing some data to it, but the problem is when this new viewController gets pushed it doesn't display the cells or rows content like labels, textfields and so on..
This is how I'm pushing from one controller to another..
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
BillComparatorViewController *vc = [[BillComparatorViewController alloc] init];
vc.selectedInstitution = [self.myInstitution objectAtIndex:indexPath.row];
vc.managedObjectContext = self.managedObjectContext;
[self.view endEditing:YES];
[self.navigationController pushViewController:vc animated:YES];
}
Then in my new pushed view controller this is my cellForRowAtIndexPath function..
//UITableView Delegate + Datasource
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
for (UIView *view in cell.contentView.subviews)
[view removeFromSuperview];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundColor = [UIColor whiteColor];
cell.textLabel.text = [self.labels objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.text = [self.labels objectAtIndex:indexPath.row];
CGFloat i = [[self.rowHeights objectAtIndex:indexPath.row] floatValue];
UILabel *cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 280, 15)];
cellLabel.font = [UIFont fontWithName:kFONTNAME size:kSystemFontSizeRows];
if ([[self.tableViewElements objectAtIndex:indexPath.row] isKindOfClass:[UIButton class]]){
cell.textLabel.font = [UIFont fontWithName:kFONTNAME size:kSystemFontSizeRows];
cell.backgroundColor = [UIColor lightGrayColor];
[cell.contentView addSubview:[self.tableViewElements objectAtIndex:indexPath.row]];
[cell.contentView addSubview:arrow];
}
else if (i > kTableRowHeight)
{
cell.textLabel.text = nil;
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, kElementWidth/2, 75)];
lbl.text = [self.labels objectAtIndex:indexPath.row];
lbl.font = _myFont;
lbl.numberOfLines = 0;
lbl.lineBreakMode = NSLineBreakByWordWrapping;
lbl.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:[self.tableViewElements objectAtIndex:indexPath.row]];
[cell.contentView addSubview:lbl];
if ([[self.tableViewElements objectAtIndex:indexPath.row] isKindOfClass:[UISwitch class]])
{
[cell.contentView addSubview:[self.tableViewElements objectAtIndex:indexPath.row]];
}
if ([[self.tableViewElements objectAtIndex:indexPath.row] isKindOfClass:[UITextField class]])
{
// Can make a class for this cell's contentview, however it never repeats in code
UITextField* txtField = [self.tableViewElements objectAtIndex:indexPath.row];
txtField.textColor = kBlueColor;
txtField.textAlignment = NSTextAlignmentLeft;
txtField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; // iOS 6 support
NSString *imageName = #"image";
NSString *arrowName = #"image1";
if (selectedRow == indexPath.row)
{
imageName = #"image";
arrowName = #"image1";
txtField.textColor = [UIColor whiteColor];
}
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(2*kElementWidth/3 -12, 23, 135, 27)];
imgView.image = [UIImage imageNamed:imageName];
[cell.contentView addSubview:imgView];
UIImageView *arrowView = [[UIImageView alloc] initWithFrame:CGRectMake(kElementWidth+10, 31, 8.5, 10)];
arrowView.image = [UIImage imageNamed:arrowName];
[cell.contentView addSubview:arrowView];
[cell.contentView addSubview:txtField];
}
}
else
{
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.text = [self.labels objectAtIndex:indexPath.row];
cell.textLabel.font = _myFontSmall;
cell.textLabel.textColor = kBlueColor;
cell.textLabel.backgroundColor = [UIColor clearColor];
if ([[self.tableViewElements objectAtIndex:indexPath.row] isKindOfClass:[UITextField class]])
{
// Can make a class for this cell's contentview, however it never repeats in code
UITextField* txtField = [self.tableViewElements objectAtIndex:indexPath.row];
txtField.textColor = kBlueColor;
txtField.textAlignment = NSTextAlignmentLeft;
txtField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; // iOS 6 support
NSString *imageName = #"image";
NSString *arrowName = #"image1";
if (selectedRow == indexPath.row)
{
imageName = #"image";
arrowName = #"image2";
txtField.textColor = [UIColor whiteColor];
}
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(2*kElementWidth/3 -12, 12, 135, 27)];
imgView.image = [UIImage imageNamed:imageName];
[cell.contentView addSubview:imgView];
UIImageView *arrowView = [[UIImageView alloc] initWithFrame:CGRectMake(kElementWidth+10, 20, 8.5, 10)];
arrowView.image = [UIImage imageNamed:arrowName];
[cell.contentView addSubview:arrowView];
[cell.contentView addSubview:txtField];
}
if (isSearching)
{
cellLabel.text = [searchElements objectAtIndex:indexPath.row];
[cell.contentView addSubview:cellLabel];
}
}
return cell;
}
It's a bit long sorry but I think it was important to post it..
UPDATE
This is how the problem happens, when user select a row within the current viewcontroller didSelectRowAtIndexPath calls a new viewcontroller and pushes this controller but if the user does this while searching with the keyboard in the screen is when the problem happens that the new viewcontroller pushed doenst show any content in its tableview..
SOLVED
Within my cellForRowAtIndexPath
[cell setTranslatesAutoresizingMaskIntoConstraints:NO];
The issue is with Autolayout. I had the same problem with iOS 8 beta. (I know you're not using i OS 8 but still).
Try turning it off. Should sort the issue out.
i think the problem is initiating the object of BillComparatorViewController.
You do like this .You may get solution
BillComparatorViewController *vc = [[BillComparatorViewController alloc] initWithNibName:"BillComparatorViewController" Bundle:nil];
I have a UITableView with paging enabled. At first I can scroll very smoothly between cells. However, after scrolling back and forth a few times, it becomes rather choppy. Am I not creating/reusing my cells properly?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cash-Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 40, cell.frame.size.width-40, cell.frame.size.width-40)];
Purchase *purch = (Purchase *)self.purchases[indexPath.row];
[image setImage:[UIImage imageWithData:purch.image]];
image.contentMode = UIViewContentModeCenter;
image.contentMode = UIViewContentModeScaleAspectFill;
image.clipsToBounds = YES;
[self setMaskTo:image byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
[cell addSubview:image];
UIView *price = [[UIView alloc] initWithFrame:CGRectMake(20, 40+cell.frame.size.width-40, cell.frame.size.width-40, 60)];
UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, price.frame.size.width, price.frame.size.height)];
[priceLabel setText:#"$10.0"];
[priceLabel setTextAlignment:NSTextAlignmentCenter];
[priceLabel setFont:[UIFont boldSystemFontOfSize:25.0f]];
[self setMaskTo:price byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight];
[price addSubview:priceLabel];
[price setBackgroundColor:[UIColor whiteColor]];
[cell addSubview:price];
return cell;
}
If you want to reuse them then make sure you add the content to your UITableViewCell only once and on reusing it just update it (e.g. update the image and the price accordingly).
Otherwise, what you get with your implementation is adding subviews price and image everytime (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath gets called. Just try debugging the number of subviews your cell has to see if I am right.
You could go this way:
if ( ! [cell viewWithTag: 1])
{
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 40, cell.frame.size.width-40, cell.frame.size.width-40)];
image.tag = 1;
image.contentMode = UIViewContentModeCenter;
image.contentMode = UIViewContentModeScaleAspectFill;
image.clipsToBounds = YES;
[self setMaskTo:image byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
[cell addSubview:image];
}
if ( ! [cell viewWithTag: 2])
{
UIView *price = [[UIView alloc] initWithFrame:CGRectMake(20, 40+cell.frame.size.width-40, cell.frame.size.width-40, 60)];
UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, price.frame.size.width, price.frame.size.height)];
priceLabel.tag = 2;
[priceLabel setTextAlignment:NSTextAlignmentCenter];
[priceLabel setFont:[UIFont boldSystemFontOfSize:25.0f]];
[self setMaskTo:price byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight];
[price addSubview:priceLabel];
[price setBackgroundColor:[UIColor whiteColor]];
[cell addSubview:price];
}
Purchase *purch = (Purchase *)self.purchases[indexPath.row];
UIImage *image = [cell viewWithTag: 1];
[image setImage:[UIImage imageWithData:purch.image]];
UILabel *priceLabel = [cell viewWithTag: 2];
[priceLabel setText:#"$10.0"];
This is because - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is being called each time the cell comes into the view. Add the images to an array (in viewDidLoad or somewhere), and you'll get a smoother result.
You are not reusing the cells, which causes the lag/choppy scrolling.
suggestion:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cash-Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(cell == nil){
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 40, cell.frame.size.width-40, cell.frame.size.width-40)];
Purchase *purch = (Purchase *)self.purchases[indexPath.row];
[image setImage:[UIImage imageWithData:purch.image]];
image.contentMode = UIViewContentModeCenter;
image.contentMode = UIViewContentModeScaleAspectFill;
image.clipsToBounds = YES;
image.tag = 10;
[self setMaskTo:image byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
[cell.contentView addSubview:image];
UIView *price = [[UIView alloc] initWithFrame:CGRectMake(20, 40+cell.frame.size.width-40, cell.frame.size.width-40, 60)];
UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, price.frame.size.width, price.frame.size.height)];
[priceLabel setText:#"$10.0"];
[priceLabel setTextAlignment:NSTextAlignmentCenter];
[priceLabel setFont:[UIFont boldSystemFontOfSize:25.0f]];
[self setMaskTo:price byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight];
[price addSubview:priceLabel];
[price setBackgroundColor:[UIColor whiteColor]];
price.tag = 11;
[cell.contentView addSubview:price];
}
//update the value here
UIImageView *imgV = (UIImageView *)[self.contentView viewWithTag:10];
// update the imageView here
//imgV.image = image
UIView *vv = (UIView *)[self.contentView viewWithTag:11];
// Update the UIView here
//vv.text = #"text here";
//Put setselection style here to allow the cell to be loaded before setting the property value
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
After adding a font and a shadow to some UILabels I noticed that the table view animation lags when the view is popped off the stack (a side swipe like FB/Path uses). The side swipe was smooth until I added the UILabel shadows.
I think I might be adding it it the wrong place so that the label properties are being added incorrectly maybe. Please take a look at the following cellForRowAtIndexPath: method below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellReuseIdentifier = #"cellReuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier];
}
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 2, self.view.bounds.size.width, 200)];
imageView.image = [UIImage imageNamed:#"rest.jpg"];
[cell.contentView addSubview:imageView];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, 320, 30)];
titleLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"title"];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor whiteColor];
[titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:24]];
titleLabel.layer.shadowColor = [[UIColor whiteColor] CGColor];
titleLabel.layer.shadowOpacity = 0.7;
[cell.contentView addSubview:titleLabel];
UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, cell.bounds.size.width, 30)];
detailLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"description"];
detailLabel.backgroundColor = [UIColor clearColor];
detailLabel.textColor = [UIColor whiteColor];
[detailLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:18]];
detailLabel.layer.shadowColor = [[UIColor whiteColor] CGColor];
detailLabel.layer.shadowOpacity = 0.7;
[cell.contentView addSubview:detailLabel];
cell.contentView.backgroundColor = [UIColor clearColor];
return cell;
}
thanks for any help.
You're always adding new subviews. So whenever you scroll the table view your cells are getting more and more content added into them.
Create all your subviews when the cell is created and then just update the subviews settings. Something like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellReuseIdentifier = #"cellReuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 2, self.view.bounds.size.width, 200)];
imageView.tag = 123123;
[cell.contentView addSubview:imageView];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, 320, 30)];
titleLabel.tag = 234234];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor whiteColor];
[titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:24]];
titleLabel.layer.shadowColor = [[UIColor whiteColor] CGColor];
titleLabel.layer.shadowOpacity = 0.7;
[cell.contentView addSubview:titleLabel];
UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, cell.bounds.size.width, 30)];
detailLabel.tag = 345345];
detailLabel.backgroundColor = [UIColor clearColor];
detailLabel.textColor = [UIColor whiteColor];
[detailLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:18]];
detailLabel.layer.shadowColor = [[UIColor whiteColor] CGColor];
detailLabel.layer.shadowOpacity = 0.7;
[cell.contentView addSubview:detailLabel];
cell.contentView.backgroundColor = [UIColor clearColor];
}
UIImageView *imageView = (UIImageView *)[cell viewWithTag:123123];
imageView.image = [UIImage imageNamed:#"rest.jpg"];
UILabel *titleLabel = (UILabel *)[cell viewWithTag:234234];
titleLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"title"];
UILabel *detailLabel = (UILabel *)[cell viewWithTag:345345];
detailLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"description"];
return cell;
}
Since text attributes never change, move the code that sets them inside the if statement. Keep only the code that sets the image and the text of your labels outside the if statement. Cells are reused, so the attributes such as font etc. will remain with the cell even after it gets "recycled". In the else branch add code that finds existing labels in the cell. Otherwise, you keep adding the same label to the cell multiple times.
You add subviews, even after dequeueing instead of initializing a new cell. Make sure all the code that creates and adds subviews are only done on initializing a cell. If you need to refer to views in the cell for configuring, subclass UITableViewCell.
Also, shadow rendering could be slowing it down too, add a shadowpath to make the rendering more efficient:
add to your tableView:cellForRowAtIndexPath: method:
...
CGPathRef shadowPath = [UIBezierPath bezierPathWithRect:detailLabel.layer.bounds].CGPath;
detailLabel.layer.shadowPath = shadowPath;
...
This article from Twitter Engineering gives you a great overview: http://engineering.twitter.com/2012/02/simple-strategies-for-smooth-animation.html
Basically, you want to avoid using subviews but instead draw your content directly with Quartz. It's the single best thing you can do to improve performance. Also: Avoid transparency! In Instruments, you can also use the Core Animation instrument and activate "Color Blended Layers" to see where transparent views are being composed:
Replace your code with this :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellReuseIdentifier = #"cellReuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 2, self.view.bounds.size.width, 200)];
imageView.image = [UIImage imageNamed:#"rest.jpg"];
imageView.tag =1;
[cell.contentView addSubview:imageView];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, 320, 30)];
titleLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"title"];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.tag = 2;
titleLabel.textColor = [UIColor whiteColor];
[titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:24]];
titleLabel.layer.shadowColor = [[UIColor whiteColor] CGColor];
titleLabel.layer.shadowOpacity = 0.7;
[cell.contentView addSubview:titleLabel];
UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, cell.bounds.size.width, 30)];
detailLabel.tag = 3;
detailLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"description"];
detailLabel.backgroundColor = [UIColor clearColor];
detailLabel.textColor = [UIColor whiteColor];
[detailLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:18]];
detailLabel.layer.shadowColor = [[UIColor whiteColor] CGColor];
detailLabel.layer.shadowOpacity = 0.7;
}
UIImageView *tempImgView = (UIImageView *)[cell viewWithTag:1];
tempImgView.image = [UIImage imageNamed:#""];// Here you can set any image by reusing imageview without allocating again and again
UILabel *tempLabel;
tempLabel = (UILabel *)[cell viewWithTag:2];
tempLabel.text = #"";// Here you can access your title label and can set its properties without allocating again
tempLabel = (UILabel *)[cell viewWithTag:3];
tempLabel.text = #"";// Here you can access your detailLabel label and can set its properties without allocating again
[cell.contentView addSubview:detailLabel];
cell.contentView.backgroundColor = [UIColor clearColor];
return cell;
}
You need create custom table view cell with a class. This code you added many label then shadow show no correctly.
code like this.
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellReuseIdentifier = #"cellReuseIdentifier";
UITableCustomViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier];
if (cell == nil)
{
cell = [[UITableCustomViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier];
}
cell.imageView.image = [UIImage imageNamed:#"rest.jpg"];
cell.titleLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"title"];
cell.detailLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"description"];
return cell;
}
I have a UITableView that has an image and text on each cell, however they are put side by side. What I'm trying to do is put the text label over the top of the image. Is it possible?
Take custom UITableViewCell,where take UIView at every cell and make your image as the background color of the UIView then take a UILabel at the top of the UIView.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
cell.textLabel.font = [UIFont fontWithName:#"Arial" size:15.0];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"pl_arows.png"]];
cell.accessoryView = imageView;
cellView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,290, 70)] autorelease]; //important do not changer the position
cellView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"order_description_bg.png"]];
cellView.tag =10;
[cell.contentView addSubview:cellView];
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 15, 39, 36)];
imgView.image = [UIImage imageNamed:#"image_category.png"];
imgView.layer.borderColor = [UIColor blackColor].CGColor;
imgView.tag = 5;
[cellView addSubview:imgView];
//Status label
CGRect statusRectText = CGRectMake(52, 0, 50, 18);
UILabel *statusLabelText = [[[UILabel alloc] initWithFrame:statusRectText] autorelease];
statusLabelText.textAlignment = UITextAlignmentRight;
statusLabelText.backgroundColor = [UIColor clearColor];
statusLabelText.tag = 101;
[imgView addSubview:statusLabelText];
}
You will have to define a custom UITableViewCell in which you can place any standard UI objects, including a UILabel on top of your UIImage.