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 !!!
Related
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];
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
I want add label to every row in tableview at right side of the row in iphone programmatically.
Can anyone help me?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
UILabel* lbl = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, h)];
lbl.font = [UIFont boldSystemFontOfSize:17.0];
lbl.text = [self.arrRows objectAtIndex:indexPath.row];
[cell.contentView addSubview:lbl];
[lbl release];
return cell;
You can achieve it 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] autorelease];
}
UILabel *lblText = [[UILabel alloc] initWithFrame:CGRectMake(200, 3, 100, 15)]; // For right alignment
lblText.text = [self.arrText objectAtIndex:indexPath.row];
lblText.textColor = [UIColor blackColor];
[cell addSubview:lblText];
[lblText release];
return cell;
}
I think you are asking for code.
try
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UILabel *yourLabel = nil;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
yourLabel = [[UILabel alloc] initWithFrame:CGRect……];
[yourLabel setTag:9999];
[[cell contentView] addSubview:addressLabel];
}
if (!yourLabel)
yourLabel = (UILabel*)[cell viewWithTag:9999];
return cell;
}
In your cellForRowAtIndexPath: method :-
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=#"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
//set reuseIdentifier:nil for avoid label overlapping
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
//make your label as according to you
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
label.backgroundColor = [UIColor redColor];
// add label content view of cell
[cell.contentView addSubview:label];
return cell;
}
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];
}