Load More Cell UITableView - ios

I'm trying to add Load More Cell to my tableview. Begin with 5 rows and when I click Load More Cell and add more 5 rows. All work fine without load more cell has image but I don't want it. I tried to fix it but it does not work.
Table begin
http://i.stack.imgur.com/Ld2ED.png
Table after click Load More Cell
http://i.stack.imgur.com/A5xjE.png
Here is my cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
#define IMAGE_VIEW_TAG 99
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (indexPath.section == 0) {
//create new cell
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//add AsyncImageView to cell
AsyncImageView *imageView = [[AsyncImageView alloc] initWithFrame:CGRectMake(5.0f, 5.0f, 50.0f, 50.0f)];
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;
imageView.tag = IMAGE_VIEW_TAG;
[cell addSubview:imageView];
[imageView release];
//common settings
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
cell.indentationWidth = 44.0f;
cell.indentationLevel = 1;
//get image view
AsyncImageView *imgView = (AsyncImageView *)[cell viewWithTag:IMAGE_VIEW_TAG];
//cancel loading previous image for cell
[[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:imgView];
//load the image
imgView.imageURL = [imageURLs objectAtIndex:indexPath.row];
//display image path
cell.textLabel.text = [tableData objectAtIndex:indexPath.row ];
}
else if (indexPath.section == 1) {
cell.textLabel.text = [NSString stringWithFormat:NSLocalizedString(#"Load More...", #"The text to display to load more content"), kNumberOfItemsToAdd];
cell.imageView.image = Nil;
cell.textLabel.textColor = [UIColor colorWithRed:0.196f green:0.3098f blue:0.52f alpha:1.f];
cell.textLabel.font = [UIFont boldSystemFontOfSize:12.f];
}
numberOfSection
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (numberOfItemsToDisplay == [tableData count]) {
return 1;
}
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// return 5;
// return tableData.count;
if (section == 0) {
return numberOfItemsToDisplay;
} else {
return 1;
}
}

replace this:
else if (indexPath.section == 1) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
...
...
...
}

Related

Add Storyboard UITableView and custom UITableViewCells to UIScrollView programmatically

In my Storboard, I have a UIViewController (ParentClass) -> UIScrollView (linked to Parent) -> UITableView (linked to Parent) -> UITableViewCell (2 cells, each with their own Class).
What I'm trying to do is add several instances of the UITableView (with the custom cells) into the UIScrollView.
I am close with the following code, but the cells are coming up empty:
self._scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
self._scrollView.pagingEnabled = YES;
self._scrollView.showsVerticalScrollIndicator = NO;
self._scrollView.alwaysBounceVertical = NO;
NSInteger numberOfViews = 22;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
UITableViewController *theTableView = [[UITableViewController alloc] initWithStyle:UITableViewStyleGrouped];
theTableView.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
theTableView.tableView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self._scrollView.frame.size.height);
theTableView.tableView.backgroundColor = [UIColor colorWithRed:246/255.0 green:248/255.0 blue:249/255.0 alpha:1];
theTableView.tableView.delegate = self;
theTableView.tableView.dataSource = self;
theTableView.tableView.tag = 100+i;
[self._scrollView addSubview:theTableView.tableView];
}
self._scrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:self._scrollView];
All the tables show up and all the pages are there and the scroll as they should, but the Custom Cell layouts are not showing up.
Here's the code I use to layout the tables:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath row] == 0) {
return 127.0;
} else {
return 56.0;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
static NSString *CellIdentifier = #"MealImageCell";
MealImageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MealImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[self configureImageCell:cell atIndexPath:indexPath];
return cell;
} else {
static NSString *CellIdentifier = #"MealInfoCell";
MealInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MealInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[self configureInfoCell:cell atIndexPath:indexPath];
return cell;
}
}
- (void)configureImageCell:(MealImageCell *)cell atIndexPath:(NSIndexPath *)indexPath {
NSMutableDictionary *dictMeal = [[NSMutableDictionary alloc] init];
if ([arrayMeals count]) {
dictMeal = [arrayMeals objectAtIndex:indexPath.section];
cell.userInteractionEnabled = YES;
} else {
cell.userInteractionEnabled = NO;
}
[cell.imageFood sd_setImageWithURL:[NSURL URLWithString:[dictMeal valueForKey:#"img_src"]]
placeholderImage:[UIImage imageNamed:[NSString stringWithFormat:#"meal%li.jpeg",(long) indexPath.section+1]]];
}
- (void)configureInfoCell:(MealInfoCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
NSString *mealName;
if ([arrayMeals count]) {
NSMutableDictionary *dictMeal = [arrayMeals objectAtIndex:indexPath.section];
cell.userInteractionEnabled = YES;
if ([dictMeal valueForKey:#"mealDescription"] == [NSNull null]) {
mealName = NSLocalizedString(#"No Name", nil);
} else if ([[dictMeal valueForKey:#"mealDescription"] isEqualToString:#""]) {
mealName = NSLocalizedString(#"Deleted", nil);
} else {
mealName = [decodeHTMLEntities decodeHTMLEntities:[[dictMeal valueForKey:#"mealDescription"] stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
}
} else {
cell.userInteractionEnabled = NO;
mealName = NSLocalizedString(#"Loaing Meal...", nil);
}
cell.labelFood.text = mealName;
}
Without the scrollView, the table is populated as it should be.
The answer is to relace:
MealImageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
with
MealImageCell *cell = [_myTablView dequeueReusableCellWithIdentifier:CellIdentifier];
with _myTableView being the IBOutlet for the table view in Storyboard.

UITableView's cell doesn't work as setting

I set uitableview's cell as following
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[DetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (indexPath.row == 0) {
cell.backgroundColor = [UIColor redColor];
UIImageView *bigphoto = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
bigphoto.image = [UIImage imageNamed:#"bigphoto.png"];
[cell addSubview:bigphoto];
}
else {
cell.backgroundColor = [UIColor blackColor];
cell.myphoto.image = [UIImage imageNamed:#"myphoto.png"];
cell.phototime.text = #"2014-03-01";
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
return 320;
}
return 220;
}
I want the first cell(indexPath.row = 0) is different from others, the first cell is set a bigphoto and its background color is red(with its UI Design), others are myphoto.png with black color background(with the UI Desigin of DetailCell), but the code runs with the wrong result,
the first cell(indexPath.row = 0) is right, but the indexPath.row = 3 or 6 or 9.. are the same as indexPath.row = 0, not like indexPath.row = 1 or 2 or 4...
so how can I fixed this? thanks
The cell is being reused, you should use different cell identifiers for cells with different representation. This should work:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier;
DetailCell *cell = nil;
if (indexPath.row == 0) {
cellIdentifier = #"Cell0";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[DetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.backgroundColor = [UIColor redColor];
UIImageView *bigphoto = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
bighoto.tag = 1;
[cell addSubview:bigphoto];
}
UIImageView *bigphoto = (UIImageView *)[cell viewWithTag:1];
bigphoto.image = [UIImage imageNamed:#"bigphoto.png"];
}
else {
cellIdentifier = #"Cell1";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[DetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.backgroundColor = [UIColor blackColor];
}
cell.myphoto.image = [UIImage imageNamed:#"myphoto.png"];
cell.phototime.text = #"2014-03-01";
}
return cell;
}
This is because the first cell is "reused." Since you are using these cells for two separate purposes, it would be much cleaner to have a prototype cell in the storyboard that you use for just the first row, and then otherwise reuse cells for the rest of the rows.
In your case best option is to use the static tabelViewCells. Please check on this link.

User Interaction Enabled changing text color and click-ability of other sections

I have 3 sections in a table view and only using the middle section, section 2, to show various cells. Sections 1 and 3 only show one cell and I am making them unclickable since I want to display buttons and text on them. I made them and it was working fine until I made sections 1 and 3 userInteractionEnabled=NO.
Code: I know I can make this Object Oriented, and it was, but once this problem came up I tried to make it differently but it is still the same.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:CGRectZero];
selectedBackgroundView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.1];
cell.selectedBackgroundView = selectedBackgroundView;
if(cell==nil) { NSLog(#"Cell is nil");}
}
if(indexPath.section == 0)
{
cell.textLabel.text = nil;
cell.accessoryView = nil;
cell.detailTextLabel.text = nil;
dosageButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
amountButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[dosageButton addTarget:self action:#selector(showDosages:) forControlEvents:UIControlEventTouchUpInside];
[amountButton addTarget:self action:#selector(showAmount) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:dosageButton];
[self.view addSubview:amountButton];
cell.userInteractionEnabled = NO;
return cell;
}
else if (indexPath.section == 1)
{
if (self.nameMutable.count != 0 )
{
cell.textLabel.text = [self.nameMutable objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [NSString stringWithFormat:#"$%#",[self.priceMutable objectAtIndex:indexPath.row]];
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"chevron"]];
return cell;
}
else
{
//Empty for now. Waiting for data fetching to finish
return cell;
}
}
else if (indexPath.section == 2)
{
cell.userInteractionEnabled = NO;
cell.accessoryView = nil;
cell.detailTextLabel.text = nil;
return cell;
}
else
{
NSLog(#"Something went wrong");
return cell;
}
}
For some reason my table view cell in section 1 where it is supposed to be clickable the color changes to a dark grey and is not clickable anymore. Its usually cell 3 and cell 10. Also, when I scroll down and Section 0 is no longer visible and then I scroll back up and Section 0 is visible, some of the cells become non-clickable and the color of the text changes.
Also, how can I make a certain cell, inside section 1, have bigger height because the text is too long to display and it starts to display "..." or covers the detailTextLabel. Thanks in advance.
You have to remember that these cells are being reused or 'recycled' so if you're setting userInteractionEnabled=NO for an if statement you need to set it to userInteractionEnabled=YES in your else statement, or set it as YES before all your statements. You also want to make sure that you're adding any other subviews (buttons etc.) that are unique to certain index paths to cells that are newly created, where you would stick that piece of code inside your if(cell==nil) statement. Something like this:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:CGRectZero];
selectedBackgroundView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.1];
cell.selectedBackgroundView = selectedBackgroundView;
if(indexPath.section == 0) {
dosageButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
amountButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[dosageButton addTarget:self action:#selector(showDosages:) forControlEvents:UIControlEventTouchUpInside];
[amountButton addTarget:self action:#selector(showAmount) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:dosageButton];
[self.view addSubview:amountButton];
}
if(cell==nil) { NSLog(#"Cell is nil");}
}
cell.userInteractionEnabled = YES;
cell.accessoryView = nil;
cell.textLabel.text = nil;
cell.detailTextLabel.text = nil;
if(indexPath.section == 0)
{
cell.userInteractionEnabled = NO;
}
else if (indexPath.section == 1)
{
if (self.nameMutable.count != 0 )
{
cell.textLabel.text = [self.nameMutable objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [NSString stringWithFormat:#"$%#",[self.priceMutable objectAtIndex:indexPath.row]];
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"chevron"]];
}
else
{
//Empty for now. Waiting for data fetching to finish
}
}
else if (indexPath.section == 2)
{
cell.userInteractionEnabled = NO;
}
else
{
NSLog(#"Something went wrong");
}
return cell;
}
And if you want to change the height of certain index paths (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath Docs delegate method.

My UITableViewCell is reusing a previous cells UIImage

I have a UITable that reuses cells. Each cell has an image dependant on a condition. The correct images are always used however when cells are reused the old image is displayed behind the new one. I have tried to remove the uiimageview before adding a new subview however it has no effect.
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpTableIden = #"simpTableIden";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpTableIden];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpTableIden];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [[self.achievements objectForKey:[keys objectAtIndex:row]] objectForKey:#"name"];
CGRect picFrame = CGRectMake(cell.frame.size.width-cell.frame.size.height, 0, cell.frame.size.height, cell.frame.size.height);
UIImage *greyDot = [UIImage imageNamed:#"greyDot.png"];
UIImage *whiteDot = [UIImage imageNamed:#"whiteDot.png"];
UIImageView *achievImg = [[UIImageView alloc] init];
tableView.backgroundColor = [UIColor clearColor];
achievImg.frame=picFrame;
if ([[[self.achievements objectForKey:[keys objectAtIndex:row]] objectForKey:#"achieved"] boolValue]) {
achievImg.image = whiteDot;
}
else {
achievImg.image = greyDot;
}
[achievImg removeFromSuperview];
[cell.contentView addSubview:achievImg];
return cell;
}
Any help would be greatly appreciated?
The problem is each time cellForRowAtIndexPath: is called you create a new imageview.
I corrected your code as follows:
Here we create an imageView once for each new new cell and replace it's image every time cellForRowAtIndexPath: is called:
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpTableIden = #"simpTableIden";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpTableIden];
UIImageView *achievImg = nil;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpTableIden];
//New Cell ... Create Image view
achievImg = [[UIImageView alloc] init];
achievImg.tag = 10;
[cell.contentView addSubview:achievImg];
}
else{
//Old cell...get previously createdd imageview
achievImg = (UIImageView*)[cell.contentView viewWithTag:10];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [[self.achievements objectForKey:[keys objectAtIndex:row]] objectForKey:#"name"];
CGRect picFrame = CGRectMake(cell.frame.size.width-cell.frame.size.height, 0, cell.frame.size.height, cell.frame.size.height);
UIImage *greyDot = [UIImage imageNamed:#"greyDot.png"];
UIImage *whiteDot = [UIImage imageNamed:#"whiteDot.png"];
tableView.backgroundColor = [UIColor clearColor];
achievImg.frame=picFrame;
if ([[[self.achievements objectForKey:[keys objectAtIndex:row]] objectForKey:#"achieved"] boolValue]) {
achievImg.image = whiteDot;
}
else {
achievImg.image = greyDot;
}
//You don't need this because it doesn't have a superview in the first place..it's new
//[achievImg removeFromSuperview];
return cell;
}
try like this,
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpTableIden = #"simpTableIden";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpTableIden];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpTableIden];
NSUInteger row = [indexPath row];
cell.textLabel.text = [[self.achievements objectForKey:[keys objectAtIndex:row]] objectForKey:#"name"];
CGRect picFrame = CGRectMake(cell.frame.size.width-cell.frame.size.height, 0, cell.frame.size.height, cell.frame.size.height);
UIImage *greyDot = [UIImage imageNamed:#"greyDot.png"];
UIImage *whiteDot = [UIImage imageNamed:#"whiteDot.png"];
UIImageView *achievImg = [[UIImageView alloc] init];
tableView.backgroundColor = [UIColor clearColor];
achievImg.frame=picFrame;
if ([[[self.achievements objectForKey:[keys objectAtIndex:row]] objectForKey:#"achieved"] boolValue]) {
achievImg.image = whiteDot;
}
else {
achievImg.image = greyDot;
}
[achievImg removeFromSuperview];
[cell.contentView addSubview:achievImg];
}
return cell;
}
You are always adding the imageView to the contentView of the cell. If the cell is reused that will lead to multiple imageViews in cell.
You can give a tag to the imageView. Always check if there is a view will same tag in the contentView of cell. If not add the imageView.
UIImageView *achievImg = (UIImageView *)[cell.contentView viewWithTag:55];
if(!achievImg){
achievImg = [[UIImageView alloc] init];
achievImg.tag = 55;
[cell.contentView addSubView:achievImg];
}

How to add images to UITableView cell?

I temporarly added some values to the UITableViewCell programmatically. But I need to add images to each cell. How can I do that?
Here is my code. .h file
#interface BidalertsViewController : UIViewController <UITableViewDelegate,UITableViewDataSource> {
NSArray *listData;
}
#property(nonatomic, retain) NSArray *listData;
#end
.m file
#synthesize listData;
- (void)viewDidLoad {
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(5,0,310,28)];
newView.backgroundColor=[UIColor whiteColor];
UITextView *mytext = [[UITextView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 25.0)];
mytext.backgroundColor = [UIColor clearColor];
mytext.textColor = [UIColor blackColor];
mytext.editable = NO;
mytext.font = [UIFont systemFontOfSize:15];
mytext.text = #"Asset Name";
[mytext release];
[newView addSubview:mytext];
[self.view addSubview:newView];
[newView release];
NSArray *array = [[NSArray alloc] initWithObjects:#"iPhone", #"iPod", #"iPad",nil];
self.listData = array;
[array release];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)dealloc {
[listData dealloc];
[super dealloc];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.listData count];
}
- (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];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
return cell;
UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)];
imv.image=[UIImage imageNamed:#"user.jpg"];
[cell.contentView addSubview:imv];
[imv release];
}
#end
The code didn't add images to the cell. What's the problem? How can I do to add custom image to cell?
Move return cell; at the end of the method:
- (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];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)];
imv.image=[UIImage imageNamed:#"user.jpg"];
[cell.contentView addSubview:imv];
[imv release];
return cell;
}
You can add images using the imageView property of a cell:
cell.imageView.image = [UIImage imageNamed:#"user.jpg"];
in your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method.
Here's an example cell loading a facebook image in a table cell.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// Leave cells empty if there's no data yet
if (nodeCount > 0)
{
// Set up the cell...
AppRecord *appRecord = [self.friendsList objectAtIndex:indexPath.row];
cell.textLabel.text = friend.name;
// Only load cached images; defer new downloads until scrolling ends
if (!appRecord.icon)
{
// set the url
appRecord.imageURLString = [NSString stringWithFormat:#"http://graph.facebook.com/%#/picture", friend.recordID];
// request the download
if (self.tableView.dragging == NO && self.tableView.decelerating == NO)
{
[self startIconDownload:appRecord forIndexPath:indexPath];
}
// if a download is deferred or in progress, return a placeholder image
cell.imageView.image = [UIImage imageNamed:#"Placeholder.png"];
}
else
{
cell.imageView.image = appRecord.icon;
}
}
return cell;
you adding it right, but you fogot one thing:
- (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];
}
NSUInteger row = [indexPath row]; cell.textLabel.text = [listData objectAtIndex:row];
UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)];
imv.image=[UIImage imageNamed:#"user.jpg"];
[cell.contentView addSubview:imv];
[imv release];
//you fogot this:
return cell;
}
// create a rectangle box for image view
UIImageView *iconImage = [[UIImageView alloc] init];
iconImage.frame = CGRectMake(12,4,53.5,53.5);
// Create a label for cells
UILabel *cellName = [[UILabel alloc] initWithFrame:CGRectMake(75,18,200,20)];
cellName.font = [self fontForBodyTextStyle];
NSString *cellNameStr = [NSString stringWithFormat:#"%#",self.tableCellNames[indexPath.row]];
// Select path row tab actions
switch (indexPath.row) {
case 0:
cellName.text = cellNameStr;
iconImage.image = [UIImage imageNamed:#"condition.png"];
[cell.contentView addSubview:iconImage];
[cell.contentView addSubview:cellName];
break;
case 1:
cellName.text = cellNameStr;
iconImage.image = [UIImage imageNamed:#"videos.png"];
[cell.contentView addSubview:iconImage];
[cell.contentView addSubview:cellName];
break;
case 2:
cellName.text = cellNameStr;
iconImage.image = [UIImage imageNamed:#"provider.png"];
[cell.contentView addSubview:iconImage];
[cell.contentView addSubview:cellName];
break;
case 3:
cellName.text = cellNameStr;
iconImage.image = [UIImage imageNamed:#"info.png"];
[cell.contentView addSubview:iconImage];
[cell.contentView addSubview:cellName];
break;
default:
NSAssert(NO, #"Unhandled value in cellForRowAtIndexPath");
break;
}
Thank VKJ,

Resources