I have a custom tableview to load news through webservice. I change image in each cell in method cellForRowAtIndexPath. After I got the right cell, I can access the image by two ways:
UIImageView *imageView = (UIImageView *)[cell viewWithTag:12];
imageView.image = news.image;
or directly:
cell.imageView.image = news.image;
when I use the second way, some images get into wrong cell while the first way give me a perfect result. It also happend in a callback method iconDownLoadFinsh.
I feel confused about it. Can anyone try to explain it? Please do not hesitate to ask questions. Thanks in advance.
- (void)iconDownLoadFinsh:(NSData *)imageData row:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
UIImageView *imageView = (UIImageView *)[cell viewWithTag:12];
imageView.image = [UIImage imageWithData:imageData];
NewsModel *newsModel = [newsArray objectAtIndex:indexPath.row];
newsModel.image = [UIImage imageWithData:imageData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"NewsCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"NewsTableViewCell" owner:self options:nil];
cell = newsTableViewCell;
newsTableViewCell = nil;
}
// read from newsModel
NewsModel *news = [newsArray objectAtIndex:indexPath.row];
UILabel *label;
label = (UILabel *)[cell viewWithTag:10];
label.text = [NSString stringWithFormat:news.title];
label = (UILabel *)[cell viewWithTag:11];
label.text = [NSString stringWithFormat:news.description];
UIImageView *imageView;
imageView = (UIImageView *)[cell viewWithTag:12];
imageView.image = news.image;
if (news.image == nil)
{
imageView.image = [UIImage imageNamed:IconPlaceHolder];
// if (self.tableView.dragging == NO && self.tableView.decelerating == NO) {
IconDownLoader *iconDownLoader = [[IconDownLoader alloc] init];
iconDownLoader.url = news.imageUrl;
iconDownLoader.delegate = self;
iconDownLoader.indexPath = indexPath;
[downloadArray addObject:iconDownLoader];
[iconDownLoader start];
// }
}
return cell;
}
I generally always reset the cell properties in the method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
So I will do the following after getting the cell
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"NewsTableViewCell" owner:self options:nil];
cell = newsTableViewCell;
newsTableViewCell = nil;
}
cell.imageView.image = nil; // Here resetting the imageview's image to nil.
Because you are reusing cells and if you do not clear old data, the cell data may get corrupt.
Related
Hi all I've custom cells in which I've UIImageView and UILabel I want to assign Image to UIImageView and text to UILabel which are in array, please can any one help me?? thanks in advance
I tried below code
- (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];
}
cell.textLabel.text =[CurArray objectAtIndex:indexPath.row];
NSString *cellIcon = [[self IconArray] objectAtIndex:indexPath.row];
UIImage *cellIconimage = [UIImage imageNamed:cellIcon];
UIImageView *imageView = [[UIImageView alloc] initWithImage:cellIconimage];
[imageView setFrame:CGRectMake(100.0, 30.0, 30.0, 30.0)];
[imageView setContentMode:UIViewContentModeScaleAspectFill];
[cell addSubview:imageView];
return cell;
}
With this code i can assign image to created imageview But I'm not able to to assign text for UILable
here IBCustomCellProjectList is an example which is UITableViewCell with XIB just follow the logic
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
IBCustomCellProjectList *cell = (IBCustomCellProjectList *) [tableView dequeueReusableCellWithIdentifier:nil];
// yourCustomeCell *cell = (yourCustomeCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];//custom cell
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"IBCustomCellProjectList" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (IBCustomCellProjectList *) currentObject;
break;
}
}
}
// Configure the cell.
cell.cellLabel.text = [yourTextArray objectAtIndex:i];
cell.cellImageView.image = [UIImage imageNamed:[yourImageArray objectAtIndex:i]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Also see this bellow tutorial..
Custom cell with advance control and design
http://www.appcoda.com/customize-table-view-cells-for-uitableview/Customize tableview Cell
:)
Use this code. Maybe this is what you want. You will have to use the method cellForRowAtIndexPath .
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:[cell reuseIdentifier]];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = customCell;
customCell = nil;
}
// Configure the cell.
cell.Label.text = [[NSNumber numberWithInt:indexPath.row+1]stringValue];
cell.Label.textColor = [UIColor blackColor];
cell.ImageView.image = [UIImage imageNamed:[CountryFlag objectAtIndex:indexPath.row]];
cell.textLabel.textColor=[UIColor whiteColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Use Following code:
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:#"cell"];
const NSInteger IMAGE_VIEW_TAG=1001;
const NSInteger LABEL_TAG=1002;
UIImageView *imageView;
UILabel *lbl;
if(cell==nil)
{
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"] autorelease];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
imageView =[[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)] autorelease];
imageView.tag=IMAGE_VIEW_TAG;
imageView.contentMode=UIViewContentModeScaleAspectFit;
[cell.contentView addSubview:imageView];
lbl=[[UILabel alloc] initWithFrame:CGRectMake(100, 20, 160, 20)];
lbl.font=[UIFont fontWithName:#"Helvetica" size:14.0];
lbl.adjustsFontSizeToFitWidth=YES;
lbl.minimumFontSize=10.0;
lbl.textAlignment=UITextAlignmentLeft;
lbl.textColor=[UIColor colorWithRed:73.0/255.0 green:73.0/255.0 blue:73.0/255.0 alpha:1.0];
lbl.backgroundColor=[UIColor clearColor];
lbl.tag=LABEL_TAG;
[cell.contentView addSubview:lbl];
}
imageView=(UIImageView*)[cell.contentView viewWithTag:IMAGE_VIEW_TAG];
lbl=(UILabel*)[cell.contentView viewWithTag:LABEL_TAG];
imageView.image=[UIImage imageNamed:#"image.png"];
lbl.text=#"Your Text";
return cell;
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 65.0;
}
I created a custom cell which contains a label and textfield.I am using this custom cell in table view. When i enter the data in text field and when i scroll the view my data is getting replaced.For every scroll it getting replaced/erased.Any one tell me where i am doing mistake.
My custom cell code goes here.Thanks!
-(UITableViewCell *)returnCellForEach:(UITableView *)tableView
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Identifier"];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:#"CustomCellToAddDetails" owner:self options:nil];
cell = customCell;
// customCell = nil;
customLabel = (UILabel *)[customCell.contentView viewWithTag:100];
}
return cell;
}
-(UITextField *)dataField
{
UITextField *textField = customField;
return textField;
}
You will need to store the data input by user somewhere related to particular indexpath and When you scroll through the cells you will first need to check if there is any dataObject corresponding to that indexpath and accordingly set the data. You can use a dictionary to store your data corresponding to particular cell. Hope it helps
I am not familiar with the method names that you are using, in my case I needed to create a custom Table view Cell Class outside the tableview class,
the class is called QRTYPECELL and following are the relevant codes:
#implementation QRTypeCell
#synthesize imageView;
#synthesize labelview;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
imageView = nil;
labelview= nil;
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,30,30)];
imageView.contentMode = UIViewContentModeCenter;
[self.contentView addSubview:imageView];
self.contentView.backgroundColor = [UIColor blackColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.textColor = [UIColor greenColor];
label.backgroundColor = [UIColor blackColor];
label.font = [UIFont systemFontOfSize:17];
self.labelview = label;
[self.contentView addSubview:label];
}
return self;}
And then in the TableView Class:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
QRTypeCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[QRTypeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];}
CurrentQRCode = (QRCode *)[fetchedResultsController objectAtIndexPath:indexPath];
NSString *icon = CurrentQRCode.parsed_icon;
NSString *string = CurrentQRCode.parsed_string;
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
NSString *filePath = [[NSBundle mainBundle] pathForResource:icon ofType:#"png"];
UIImage *image_file = [UIImage imageWithContentsOfFile:filePath];
in IB you see a Identifier field place any value in that and use that value as a reuse Identifier.
i think this will helps you.
ok i got it did you create class for customCell and replace this line
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Identifier"];
with
CustomCell *customCell=(CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
I'm sure this is a question that been asked and answered, but I can't seem to find what I need. I am trying to customize the background of my UITableView, so to do this, I created a subclass of UITableViewCell, and built my custom cell with the IB. I successfully connected the custom cell to the table view, so the cell I get is getting displayed. I am trying to change the background of each cell, so here is what I use:
if(row == 1){
rowBackground = [UIImage imageNamed:#"VehicleExpenses_button.png"];
selectionBackground = [UIImage imageNamed:#"VehicleExpenses_button_selected.png"];
((UIImageView *)cell.backgroundView).image = rowBackground;
// ((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;
}
within the
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
method of my Controller class. I checked with the debugger, and all the lines in the code are getting called...
Any ideas?
This is the entire method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"HomeViewCell";
static NSString *CellNib = #"HomeViewCell";
UIImage *rowBackground;
UIImage *selectionBackground;
NSInteger row = [indexPath row];
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
cell = (UITableViewCell *)[nib objectAtIndex:0];
}
if(row == 1){
rowBackground = [UIImage imageNamed:#"VehicleExpenses_button.png"];
selectionBackground = [UIImage imageNamed:#"VehicleExpenses_button_selected.png"];
((UIImageView *)cell.backgroundView).image = rowBackground;
// ((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;
}
// perform additional custom work...
return cell;
}
Maybe I'm wrong but I don't see where you initialized the backgroundView as a UIImageView. Here is what I would add to the lines where you create the cells:
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
cell = (UITableViewCell *)[nib objectAtIndex:0];
cell.backgroundView = [[[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, tableView.bounds.size.width, cellHeight)] autorelease];
cell.selectedBackgroundView = [[[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, tableView.bounds.size.width, cellHeight)] autorelease];
}
Note: replace "cellHeight" with your desired height.
I hope this helps.
You can use this:
UIImageView *cellBackGround = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"selectedRow.png"]];
[[tableView cellForRowAtIndexPath:indexPath] setBackgroundView:cellBackGround];
[cellBackGround release];
Small problem. Instead of casting and setting the backgroundView and selectedBackgroundView's image property, set them to the UIImageView created from your images.
cell.backgroundView = [[[UIImageView alloc] initWithImage:rowBackground] autorelease];
cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:selectionBackground] autorelease];
I'm trying to figure out how to display a particular type of image in a cell based on the text contents of the cell. For example, this method from the tutorial book I am working with will allow me to display the star image for the first 3 cells:
- (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];
if (row < 3) {
UIImage *image = [UIImage imageNamed:#"star.png"];
cell.imageView.image = image;
}
cell.textLabel.text = [listData objectAtIndex:row];
cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
return cell;
}
Is there any way I can get the actual cell contents here? So For example, if the cell contained the text 'bob', i could display a different image rather than a star?
Well it seems like you can look at your listData object and decide there. So you can try something like this
- (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];
if ([[listData objectAtIndex:row] isEqualToString:#"bob"]) {
UIImage *image = [UIImage imageNamed:#"bob.png"];
cell.imageView.image = image;
}
else if (row < 3) {
UIImage *image = [UIImage imageNamed:#"star.png"];
cell.imageView.image = image;
}
cell.textLabel.text = [listData objectAtIndex:row];
cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
return cell;
}
Maybe you should setup a dictionary that contains the names of the people as the key for the dictionary and the image name as the value. Then you could grab the cell's text and use that as the lookup for the dictionary.
After you set the text:
if([cell.textLabel.text rangeOfString:#"bob"].location != NSNotFound) {
UIImage *image = [UIImage imageNamed:#"bob.png"];
cell.imageView.image = image;
}
else {
UIImage *image = [UIImage imageNamed:#"star.png"];
cell.imageView.image = image;
}
My UITableView nedd to have, in the first row, an image.
What's the problem? that when the user scrolls down the tableview and, after, he scrolls up, over the image there are the informations of the other rows!
Do you know why?
This is my code (i'm using a UITableViewCell)
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row != 0) {
return 73.0;
}
else {
return 109.0;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
if (indexPath.row == 0) {
[[NSBundle mainBundle] loadNibNamed:#"customcell_3" owner:self options:NULL];
cell = cellaNib_with_image;
}
else {
[[NSBundle mainBundle] loadNibNamed:#"customcell_2" owner:self options:NULL];
cell = cellaNib;
}
}
if (indexPath.row == 0) {
UIImage *rowBackground;
UIImage *selectionBackground;
rowBackground = [UIImage imageNamed:#"image.png"];
selectionBackground = [UIImage imageNamed:#"image.png"];
cell.backgroundView = [[[UIImageView alloc] init] autorelease];
((UIImageView *)cell.backgroundView).image = rowBackground;
cell.selectedBackgroundView = [[[UIImageView alloc] init] autorelease];
((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;
}
else {
NSString *elemento = [NSString stringWithFormat:#"%#", [array objectAtIndex:indexPath.row]];
UILabel *testoLabel = (UILabel*)[cell viewWithTag:1];
testoLabel.text = elemento;
//ecc... here a take the other datas
}
return cell;
}
Thanks!
When cells are scrolled, they are reused.
So, first cell may be reused for other cells, and vice versa.
I would use two CellIdentifiers, one for first row, and second for rest of the rows.
If indexPath.row == 0, create/dequeue a cell using CellID1, and configure that and return.
If indexPath.row >1, create/dequeue using CellID2, configure this and return.
If you want to keep using single cellID, then before configuring them, nil/reset all the content first so that previous data are removed.