in the below code i am trying to show an image on the selected row and removing the image from the previously selected row. On scrolling the tableview up and down the selected image start to appear on random rows also instead of just showing on the one selected. Can anyone here please help me fix it. I am testing this on ios7 device.
Thanks.
- (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];
UIImage *image = [UIImage imageNamed:#"btn_checkmark_off.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(270, 15, 17, 17);
imageView.tag = CELL_IMGVIEW_TG;
[cell.contentView addSubview:imageView];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.prevRowIndex != indexPath.row)
{
NSIndexPath *pPrevIndexPath = [NSIndexPath indexPathForRow:self.prevRowIndex inSection:indexPath.section];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:pPrevIndexPath];
UIImageView* pImgView = (UIImageView*)[cell.contentView viewWithTag:CELL_IMGVIEW_TG];
pImgView.image = [UIImage imageNamed:#"btn_checkmark_off.png"];
// update index
self.prevRowIndex = indexPath.row;
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIImageView* pImgView = (UIImageView*)[cell.contentView viewWithTag:CELL_IMGVIEW_TG];
pImgView.image = [UIImage imageNamed:#"btn_checkmark_on.png"];
}
Try this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(270, 15, 17, 17)];
imageView.tag = CELL_IMGVIEW_TG;
[cell.contentView addSubview:imageView];
}
UIImageView* pImgView = (UIImageView*)[cell.contentView viewWithTag:CELL_IMGVIEW_TG];
if (self.prevRowIndex == indexPath.row) {
pImgView.image = [UIImage imageNamed:#"btn_checkmark_on.png"];
}
else{
pImgView.image = [UIImage imageNamed:#"btn_checkmark_off.png"];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.prevRowIndex = indexPath.row;
[self.tableView reloadData];
}
And you'd better replace prevRowIndex with selectedRowIndex which will be clear for understanding.
try replacing this line:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
by
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
Related
I tried so many examples But doesn't works.Please check my code and give solutions.Thanks in advance.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
}
if (indexPath.section==0)
{
img=[UIButton buttonWithType:UIButtonTypeRoundedRect];
img.frame=CGRectMake(25, 15, 75, 75);
img.layer.cornerRadius=6.0;
img.clipsToBounds=YES;
img.layer.borderColor=[[UIColor grayColor]CGColor];
[img setImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
[cell.contentView addSubview:img];
[img addTarget:self action:#selector(takePic:) forControlEvents:UIControlEventTouchDown];
img.titleLabel.textColor=[UIColor blackColor];
text=[[UITextField alloc]initWithFrame:CGRectMake(105, 30, 200, 30)];
text.placeholder=#"Name";
[text setBorderStyle:UITextBorderStyleRoundedRect];
[cell addSubview:text];
[cell addSubview:img];
else if (indexPath.section==1)
{
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(20, 15, 100, 20)];
label.text=[dosageArray objectAtIndex:indexPath.row];
label.backgroundColor=[UIColor clearColor];
label.font=[UIFont boldSystemFontOfSize:15.0];
label.textColor=[UIColor blackColor];
[cell addSubview:label];
return cell;
}}
I have a three section in the tableview.In first Section I have a textfield and imageview If I scroll the textfield and imageview will disappear that is the problem
Note : if you have limited data then use SOLUTION - 1 because this is bad for memory management , Other wise SOLUTION - 2 is good for you. (Solution - 1 may be helpful for u)
SOLUTION - 1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"S%1dR%1d",indexPath.section,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"NameOFData ";
return cell;
}
SOLUTION - 2
- (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];
}
cell.textLabel.text = #"NameOFData ";
return cell;
}
EDITED :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"S%1dR%1d",indexPath.section,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
if (indexPath.section==0)
{
img=[UIButton buttonWithType:UIButtonTypeRoundedRect];
img.frame=CGRectMake(25, 15, 75, 75);
img.layer.cornerRadius=6.0;
img.clipsToBounds=YES;
img.layer.borderColor=[[UIColor grayColor]CGColor];
[img setImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
[cell.contentView addSubview:img];
[img addTarget:self action:#selector(takePic:) forControlEvents:UIControlEventTouchDown];
img.titleLabel.textColor=[UIColor blackColor];
text=[[UITextField alloc]initWithFrame:CGRectMake(105, 30, 200, 30)];
text.placeholder=#"Name";
[text setBorderStyle:UITextBorderStyleRoundedRect];
[cell addSubview:text];
[cell addSubview:img];
}
}
cell.textLabel.text = #"NameOFData ";
return cell;
}
Try this code add NSMutableArray to all.Here displayArray is NSMutableArray.
text.text=[displayArray objectAtIndex:1];
remove cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier]; from else part & leave it blank
Enjoy Programming!!
Please try to use this one...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
}
return cell;
}
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = #"Hello";
return cell;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #""// value from your datasource
return cell;
}
SOLUTION 1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
//your code
}
SOLUTION 2
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
}
cell.textLabel.text= #"YOUR TEXT";
}
Hope this will help you.
All the best !!!
In my app I am having UITableView in one of my UIView.I added a background image for all cells. My problem is , when I select a particular cell,I want to change the background image of that particular cell alone and all the other cells should have a old background image.
How can i do this. Please share your ideas.
Here is my cellForRowAtIndexPath 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] autorelease];
}
// Configure the cell.
UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"MenuItem3"]];
cell.backgroundView = bgView;
cell.textLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:14];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.text = [arrayValue objectAtIndex:indexPath.row];
[cell setAccessoryType:UITableViewCellAccessoryNone];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
No Need to reload all your table just reload PreviousInxed 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:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"bck.png"]];
UITableViewCell *celld = (UITableViewCell *)[tableView cellForRowAtIndexPath:table.indexPathForSelectedRow];
celld.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"raj_star.png"]];
return cell;
}
NSIndexPath *perviouseIndexPathaf;
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (perviouseIndexPathaf)
{
[table reloadRowsAtIndexPaths:[NSArray arrayWithObject:perviouseIndexPathaf]
withRowAnimation:UITableViewRowAnimationNone];
}
UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"raj_star.png"]];
perviouseIndexPathaf = indexPath;
}
You can use the following code.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView.tag == 3)// We are checking if it's the desire tableview. If you have only one tableview then you can remove this if.
{
[tableView reloadData];
UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.backgroundView = bgView; // Here set the particular image u want.
}
}
Try this code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *objCustomCell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"NewImage"]];
objCustomCell.backgroundView = bgView;
}
in addition to your code, add the following code.
UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"SelectedMenuItem3"]];
cell.selectedBackgroundView = bgView;
For more info, check the documentation for UITableViewCell's selectedBackgroundView
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 need to put two images into a iOS tableview.
I used this code to place the image in a cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UIImage *cellImage = [UIImage imageNamed:#"verde_diversos.png"];
cell.imageView.image = cellImage;
cell.textLabel.text = [self.colorNames objectAtIndex: [indexPath row]];
return cell;
}
Help?
You'll want to create your own UITableViewCell subclass. In its initialiser you would set up two UIImageViews and add them to the contentView as subviews of it. Then in layoutSubviews you would set the frames of the UIImageViews based on how you want them laid out.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UIImageView *cellView = [[[UIImageView alloc] initWithFrame:CGRectMake(0,0,100,100)] autorelease];
cellView.tag = 555;
[cell.contentView addSubview:cellView];
}
UIImage *cellImage = [UIImage imageNamed:#"verde_diversos.png"];
cell.imageView.image = cellImage;
cell.textLabel.text = [self.colorNames objectAtIndex: [indexPath row]];
UIImageView *secondImage = [cell.contentView viewWithTag:555];
secondImage.image = [UIImage imageNamed:#"logo.png"];
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//static NSString *CellIdentifier = #"Cell";
NSString *CellIdentifier = [NSStringstringWithFormat:#"S%1dR%1d",indexPath.section,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
if (indexPath.section==0)
{
if (indexPath.row==0)
{
UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 20)];
[Name setText:#"First Name "];
Name.tag=kViewTag;
[cell.contentView addSubview:Name];
//cell.textLabel.text=#"First Name";
UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 30)];
textName.placeholder=#"Enter First Name";
textName.tag=kViewTag;
[textName setBorderStyle:UITextBorderStyleRoundedRect];
//textName.tag=0;
textName.clearButtonMode = UITextFieldViewModeWhileEditing;
textName.keyboardType=UIKeyboardTypeDefault;
textName.returnKeyType=UIReturnKeyDone;
textName.delegate=self;
textName.clearsOnBeginEditing=YES;
[cell.contentView addSubview:textName];
}
}
return cell;
}
in this code there are some more textfields also.
my problem is that when i am scrolling my table value in the textfield removes automatically..
can anyone help me???
To add a textField in a cell make your own UITableViewCell like this.
Update :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"YourCellId";
YourCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [YourCell cellFromNib];
}
cell.yourTextField.tag = indexPath.row;
cell.yourTextField.text = (NSString*)[self.textFieldValues objectAtIndex:indexPath.row];
return cell;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self.textFieldValues replaceObjectAtIndex:textField.tag withObject:textField.text];
}