I am using custom cell for my table view. In my cell , I have imageview & button. I am displaying 3 images in single row. When I select button , I am using checkbox image & when again tap on button , it deselect the checkbox image.When I select the button in first row & scroll the tabelview , it also checks the button on 3rd or 4th row. I think this is due to REUSE of cell. Here is my code for cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"LibraryElementsCell";
LibraryElementsCell *cell = (LibraryElementsCell *) [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *cellArray=[[NSBundle mainBundle] loadNibNamed:#"LibraryElementsCell" owner:self options:nil];
cell=[cellArray objectAtIndex:0];
[cell.firstElementButton setImage:[UIImage imageNamed:CHECKBOX_UNCHECKED_IMAGE] forState:UIControlStateNormal];
[cell.firstElementButton addTarget:self action:#selector(checkBoxSelectedOnLibraryElement:event:) forControlEvents:UIControlEventTouchUpInside];
[cell.secondElementButton setImage:[UIImage imageNamed:CHECKBOX_UNCHECKED_IMAGE] forState:UIControlStateNormal];
[cell.secondElementButton addTarget:self action:#selector(checkBoxSelectedOnLibraryElement:event:) forControlEvents:UIControlEventTouchUpInside];
[cell.thirdElementButton setImage:[UIImage imageNamed:CHECKBOX_UNCHECKED_IMAGE] forState:UIControlStateNormal];
[cell.thirdElementButton addTarget:self action:#selector(checkBoxSelectedOnLibraryElement:event:) forControlEvents:UIControlEventTouchUpInside];
tableView1.backgroundColor = [UIColor clearColor];
tableView1.separatorStyle = UITableViewCellSeparatorStyleNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSMutableArray *tempArray = [self.categoriesArray objectAtIndex:indexPath.section];
int row = indexPath.row * 3;
if (row <= [tempArray count])
{
LibraryElement *libElement = [tempArray objectAtIndex:row];
cell.firstElementImageView.image = [UIImage imageNamed:libElement.imageName];
}
if ((row + 1) < [tempArray count])
{
LibraryElement *libElement = [tempArray objectAtIndex:row+1];
cell.secondElementImageView.image = [UIImage imageNamed:libElement.imageName];
}
else {
[cell.secondElementImageView setHidden:YES];
[cell.secondElementButton setHidden:YES];
}
if ((row + 2) < [tempArray count])
{
LibraryElement *libElement = [tempArray objectAtIndex:row+2];
cell.thirdElementImageView.image = [UIImage imageNamed:libElement.imageName];
}
else {
[cell.thirdElementImageView setHidden:YES];
[cell.thirdElementButton setHidden:YES];
}
return cell;
}
Also some times hiding logic of button & imageview gets meessed up. It hides the imageview & button even if data is available.
Any knid of help is highly appreciated.Thanks
You need to refresh the cell components each time
e.g. create a refresh method inside the cell which takes your data and draw the cell components
refreshCellView:(NSDictionary)data
and inside cellForRowAtIndexPath call refreshCellView: after getting the cell
Related
I have a table view and in each row there is a add button on click the new row adding below i want that onclick the row has one text field in it and a user can enter a texh on it can you help me how to add a text field in the adding row.
here is my code
-(void)buttonclicked:(UIButton *)sender
{
UIButton *btn = (UIButton *)[self.view viewWithTag:sender.tag];
NSLog(#"clickedCell=%i", btn.tag);
[[NSUserDefaults standardUserDefaults]setValue:[NSString stringWithFormat:#"%i", btn.tag] forKey:#"btntag"];
[self.choices insertObject:#"newrow" atIndex:btn.tag+1];
[self.tableView reloadData];
}
and the table view method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
button.tag = indexPath.row;
button.frame = CGRectMake(280.0, 10, 25, 30.0); // x,y,width,height
[button addTarget:self action:#selector(buttonclicked:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:cell action:nil];
longPress.delegate = self;
[cell addGestureRecognizer:longPress];
int count = 0;
if(self.editing && indexPath.row != 0)
count = 1;
NSLog([NSString stringWithFormat:#"%i,%i",indexPath.row,(indexPath.row-count)]);
// Set up the cell...
if(indexPath.row == ([_choices count]) && self.editing)
{
cell.textLabel.text = #"ADD";
return cell;
}
NSString *choice = [self.choices objectAtIndex:indexPath.row];
cell.textLabel.text = choice;
return cell;
}
try like this may be it helps to you,
1.take one global variable type integer for example position.
2.when you click on particular button -(void)buttonclicked:(UIButton *)sender method will call in this method you will assign the btn.tag value to the position .
3.after that you are reloading the table view in the tableview cellForRowAtIndexPath method take one condition like
if(position==indexpath.row)
{
//add your textfield here
}.
I am new to iphone.I am struck in my project at some task (i.e),I have a table view with 66 rows.In that i am placed different book names for each cell and place a download button to each book.My requirement is when we click on download button it shows the progress view in that particular cell only but i am getting in that particular cell but when i am drag the tableview it will shows the progress views in some that cells also.It is because of dequeue reusability concept but i dont know how to avoid this problem.I want even after drag the tableview it shows the progress view on the cell which i am click the download button (cell)
here is my code below..
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 66;
}
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UIButton *downloadButton = nil;
CustomCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
//here custom cell is another class in that we have the title label declaration
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
downloadButton = [UIButton buttonWithType:UIButtonTypeCustom];
downloadButton.frame = CGRectMake(220,10,50,30);
[downloadButton setImage:[UIImage imageNamed:#"download.png"] forState:UIControlStateNormal];
[downloadButton addTarget:self action:#selector(downloadButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
downloadButton.backgroundColor = [UIColor clearColor];
downloadButton.userInteractionEnabled = YES;
downloadButton.highlighted = YES;
downloadButton.tag = indexPath.row;
NSLog(#"tag is %d",indexPath.row);
[cell.contentView addSubview:downloadButton];
}
NSString *titleLabel = [[appDelegate getBookNames]objectAtIndex:indexPath.row];
cell.TitleLabel.text = titleLabel;
return cell;
}
-(void)downloadButtonClicked:(id)sender{
int index = [sender tag];
NSLog(#"index of the cell is %d",index);
UIButton *button = (UIButton*)sender;
UITableViewCell *cell = (UITableViewCell *)[[button superview] superview];
UILabel *titleLabel = (UILabel *)[cell viewWithTag:100];
NSLog(#"label text =%#",titleLabel.text);
selectedBookTitle = titleLabel.text;
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSMutableArray *allDownloadLinks;
biblePlayerViewController = [[BiblePlayerViewController alloc]init];
allDownloadLinks = [biblePlayerViewController allDownloadLinks];
NSLog(#"all Download Links are %#",allDownloadLinks);
biblePlayerViewController.indexOfSelectedBookTitle = [[appDelegate getBookNames]indexOfObject:selectedBookTitle];
Download* download = [Download downloadWithTitle:selectedBookTitle url:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.audiotreasure.com/%#.zip",[allDownloadLinks objectAtIndex:(biblePlayerViewController.indexOfSelectedBookTitle)]]]PathtoSave:documentsPath];
[[DownloadManager sharedDownloadManager] queueDownload: download];
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
progressView.frame = CGRectMake(10, 40, 300, 20);
[tableViewCell.contentView addSubview:progressView];
}
screen shot of my project is [output of my above code which is in simulator]
you should nil the cell every time in cellForRow. This way it will not be re-used and allocated every time. It should work pretty fine in your case as your tableview is not very large. Just add the following line before cell == nil check:
cell = nil;
It should work now.
I have the same problem, one way to avoid it is to just lock the scrolling ability of the table during the download.
i am using custom checkbox buttons in my table view and want to save the selected cell's data to a mutable array.... can anyone help me... Thanks
create a mutable array to store your selected data, lets call it 'yourSeparatedData' , set the tag of your checkbox in cellForRowAtIndexPath and set onCheck: method as target. the code will look like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = #"setMe";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyle………
}
checkBox = [UIButton buttonWithType:UIButtonTypeRoundedRect];
checkBox.frame = CGRectMake(customizeMe);
if(yourSeparatedData && [yourSeparatedData indexOfObject:[yourTableViewDataSource objectAtIndex:indexPath.row]] != NSNotFound)
{
[checkBox setBackgroundImage:[UIImage imageNamed:#"check.png"] forState:UIControlStateNormal];
}
else {
[checkBox setBackgroundImage:[UIImage imageNamed:#"unCheck.png"] forState:UIControlStateNormal];
}
[checkBox addTarget:self action:#selector(onCheck:) forControlEvents:UIControlEventTouchUpInside];
[checkBox setTag:indexPath.row];
[cell addSubview:checkBox];
return cell;
}
-(void)onCheck:(id)sender {
if(yourSeparatedData && [yourSeparatedData indexOfObject:[yourTableViewDataSource objectAtIndex:[sender tag]]] != NSNotFound)
{
[sender setBackgroundImage:[UIImage imageNamed:#"unCheck.png"] forState:UIControlStateNormal];
[yourSeparatedData removeObject:[yourTableViewDataSource objectAtIndex:[sender tag]]];
}
else {
[sender setBackgroundImage:[UIImage imageNamed:#"check.png"] forState:UIControlStateNormal];
[yourSeparatedData addObject:[yourTableViewDataSource objectAtIndex:[sender tag]]];
}
[yourTableView reloadData];
}
this code is not tested, you are using checkbox so I assumed that you want to separate not just one data, at the end of the selection, you will have 'yourSeparatedData' with the objects picked from your tableView.
you can try custom action in UITableView cell for button prees,you can also use put a check box image and on click you can change the image as checked here is the code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:#"identifire"];
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"identifire"] autorelease];
cell.detailTextLabel.text=[id_Array objectAtIndex:indexPath.row];
cell.detailTextLabel.hidden=YES;
button = [UIButton buttonWithType:UIButtonTypeCustom];
NSString *path1 = [[NSBundle mainBundle] pathForResource:#"n_cross" ofType:#"png"];
UIImage *buttonImage1 = [[UIImage alloc] initWithContentsOfFile:path1];
[button setImage:buttonImage1 forState:UIControlStateNormal];
[button addTarget:self
action:#selector(customActionPressed:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Custom Action" forState:UIControlStateNormal];
button.frame = CGRectMake(245.0f, 20.0f, 40.0f, 40.0f);
[cell addSubview:button];
[buttonImage1 release];
CGRect imageFrame=CGRectMake(10,8,50,50);
self.cellimage=[[[UIImageView alloc] initWithFrame:imageFrame] autorelease];
self.cellimage.image=[imageIdArray objectAtIndex:indexPath.row];
[cell.contentView addSubview:self.cellimage];
return cell;
}
-(void)customActionPressed :(id)sender
{
//Get the superview from this button which will be our cell
UITableViewCell *owningCell = (UITableViewCell*)[sender superview];
NSIndexPath *cell = [_tableView indexPathForCell:owningCell];
NSString *uid=[id_Array objectAtIndex:cell.row];
[id_Array removeObjectAtIndex:cell.row];
[_tableView reloadData];
[self performSelectorInBackground:#selector(ignoreRequest:) withObject:uid];
}
here i have an id_Array and on selection of a cell i just remove the object at that index
You have to do this manually, there is no facility in a UITableView or UITableViewController t o do this automatically.
When user selects any cell then in didSelectRowAtIndexPath you can add selected object dynamically.
[someMutableArr addObject:[tableArr objectAtIndex:indexPath.row]];
Try this
- (void)onButtonClick {
int numberOfSections = [tableView numberOfSections];
for (int section = 0; section < numberOfSections; section++) {
int numberOfRows = [tableView numberOfRowsInSection:section];
for (int row = 0; row < numberOfRows; row++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
// Cell is selected
//here you can add that values into an array
} else {
// Cell is not selected
}
}
}
}
I am trying to have a button for selected rows of my table.
Here is the example code I am using:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ControlRowIdentifier = #"ControlRowIdentifier";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:ControlRowIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:ControlRowIdentifier] autorelease];
}
if ([indexPath row] > 5) {
UIImage *buttonUpImage = [UIImage imageNamed:#"button_up.png"];
UIImage *buttonDownImage = [UIImage imageNamed:#"button_down.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0, 0.0, buttonUpImage.size.width, buttonUpImage.size.height);
[button setBackgroundImage:buttonUpImage forState:UIControlStateNormal];
[button setBackgroundImage:buttonDownImage forState:UIControlStateHighlighted];
[button setTitle:#"Tap" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = button;
}
NSUInteger row = [indexPath row];
NSString *rowTitle = [list objectAtIndex:row];
cell.textLabel.text = rowTitle;
return cell;
}
This code works absolutely fine when loaded for 1st time. So, as per the logic it shows 'Tap' button for all rows greater than 5.
Problem occurs when I scroll up and down. Once I do that it just starts putting that button at any random row. I dont understand why it does that and it'll be really helpful if someone can give some tips on this.
Thanks.
The problem is reusing cell's identifier. In your case, the cell with an index of less than 6 must be with one identifier, and the rest from other.
Table view cells is reusable objects and you must do some clean work with it. Try use next:
if ([indexPath row] > 5) {
...
} else {
cell.accessoryView = nil;
}
I am having an issue where I am trying to save whether a repeat image will show selected or not (it is created as a UIButton on a UITableViewCell created in IB).
The issue is that since the cell gets re-used randomly, the image gets reset or set somewhere else after you start scrolling. I've looked all over and the advice was to setup an NSMutableArray to store the button's selection state and I am doing that in an array called checkRepeatState
My question is: where do I put the if-then-else statement in the code to where it will actually change the button image based on if the checkRepeatState is set to 0 or 1 for the given cell that is coming back into view? Right now, the if-then-else statement I am using has absolutely no effect on the button image when it comes into view. I'm very confused at this point.
Thank you ahead of time for any insight you can give!!!
My code is as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// set up the cell
static NSString *CellIdentifier = #"PlayerCell";
PlayerCell *cell = (PlayerCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
[[NSBundle mainBundle] loadNibNamed:#"PlayerNibCells" owner:self options:nil];
cell = tmpCell;
self.tmpCell = nil;
NSLog(#"Creating a new cell");
}
// Display dark and light background in alternate rows -- see tableView:willDisplayCell:forRowAtIndexPath:.
cell.useDarkBackground = (indexPath.row % 2 == 0);
// Configure the data for the cell.
NSDictionary *dataItem = [soundCategories objectAtIndex:indexPath.row];
UILabel *label;
label = (UILabel *)[cell viewWithTag:1];
label.text = [dataItem objectForKey:#"AnimalName"];
label = (UILabel *)[cell viewWithTag:2];
label.text = [dataItem objectForKey:#"Description"];
UIImageView *img;
img = (UIImageView *)[cell viewWithTag:3];
img.image = [UIImage imageNamed:[dataItem objectForKey:#"Icon"]];
NSInteger row = indexPath.row;
NSNumber *checkValue = [checkRepeatState objectAtIndex:row];
NSLog(#"checkValue is %d", [checkValue intValue]);
// Reusing cell; make sure it has correct image based on checkRepeatState value
UIButton *repeatbutton = (UIButton *)[cell viewWithTag:4];
if ([checkValue intValue] == 1) {
NSLog(#"repeatbutton is selected");
[repeatbutton setImage:[UIImage imageNamed:#"repeatselected.png"] forState:UIControlStateSelected];
[repeatbutton setNeedsDisplay];
} else {
NSLog(#"repeatbutton is NOT selected");
[repeatbutton setImage:[UIImage imageNamed:#"repeat.png"] forState:UIControlStateNormal];
[repeatbutton setNeedsDisplay];
}
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
}
[repeatbutton setImage:[UIImage imageNamed:#"repeatselected.png"] forState:UIControlStateSelected];
Are you also setting the button state to selected? If you just want the button to display a different image, use UIControlStateNormal. If you have set both a normal and selected image, then just set the selected state with repeatbutton.selected = YES.
Just wanted to share the working code with the help of drawnonward.. thanks again! hopefully other people can get helped by this code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// set up the cell
static NSString *CellIdentifier = #"PlayerCell";
PlayerCell *cell = (PlayerCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
[[NSBundle mainBundle] loadNibNamed:#"PlayerNibCells" owner:self options:nil];
cell = tmpCell;
self.tmpCell = nil;
NSLog(#"Creating a new cell");
// this will setup the button images for each state initially to be changed later
UIButton *repeatbutton;
repeatbutton = (UIButton *)[cell viewWithTag:4];
[repeatbutton setImage:[UIImage imageNamed:#"repeat.png"] forState:UIControlStateNormal];
[repeatbutton setImage:[UIImage imageNamed:#"repeatselected.png"] forState:UIControlStateSelected];
}
// Display dark and light background in alternate rows -- see tableView:willDisplayCell:forRowAtIndexPath:.
cell.useDarkBackground = (indexPath.row % 2 == 0);
// Configure the data for the cell... this gets called when the row scrolls into view each and every time
NSDictionary *dataItem = [soundCategories objectAtIndex:indexPath.row];
UILabel *label;
label = (UILabel *)[cell viewWithTag:1];
label.text = [dataItem objectForKey:#"AnimalName"];
label = (UILabel *)[cell viewWithTag:2];
label.text = [dataItem objectForKey:#"Description"];
UIImageView *img;
img = (UIImageView *)[cell viewWithTag:3];
img.image = [UIImage imageNamed:[dataItem objectForKey:#"Icon"]];
NSInteger row = indexPath.row;
NSNumber *checkValue = [checkRepeatState objectAtIndex:row];
NSLog(#"checkValue is %d", [checkValue intValue]);
// Reusing cell; make sure it has correct image based on checkRepeatState value
UIButton *repeatbutton = (UIButton *)[cell viewWithTag:4];
if ([checkValue intValue] == 1) {
NSLog(#"repeatbutton is selected");
[repeatbutton setSelected:YES];
} else {
NSLog(#"repeatbutton is NOT selected");
[repeatbutton setSelected:NO];
}
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
}
and my repeat button function that gets called:
- (void)repeatButton:(UIButton *)sender {
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
NSInteger row = indexPath.row;
// method for finding what button was pressed and changing the button image for repeat on/off
UITableViewCell *cell = (UITableViewCell*)[[sender superview] superview];
UIButton *repeatbutton;
repeatbutton = (UIButton *)[cell viewWithTag:4];
if ([sender isSelected]) {
[checkRepeatState replaceObjectAtIndex:row withObject:[NSNumber numberWithBool:NO]];
[sender setSelected:NO];
} else {
[checkRepeatState replaceObjectAtIndex:row withObject:[NSNumber numberWithBool:YES]];
[sender setSelected:YES];
}
}