UITableview Problem with button image - iphone

my problem is i have one tableview displaying four cells, in those cells i have one cutom button also. By clicking on that button the button image has to be changed like checkmark image.
i tried but not getting properly. my req is if i click on cell at index zero that index button image has to be changed.
can anyone help me on this.
Thanks in advance.

write the selectedIndex in .h file
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
if(selectedindex==indexPath.row){
cell.accessoryType=UITableViewCellAccessoryCheckmark;
//here you can change the backgroundimage.
button]setbackgroundImage:[UIImage imageNamed:#"1.Jpg"];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
selectedIndex=indexPath.row;
[tableView reloadData];
}

you need to do two things
You need to pass a msg to button
[comboButton setBackgroundImage:[UIImage imageNamed:#"firstImage.png"] forState:UIControlStateNormal]
[comboButton setBackgroundImage:[UIImage imageNamed:#"second.png"] forState:UIControlStateHighlighted]
and in tableView delegate method tableView:didSelectRowAtIndexPath: set the button state to Highlighted

Related

Change a label in a prototype cell upon button press

I have set up a tableview using custom cells as such:-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{SWHNearYouCell *cell = (SWHNearYouCell *)[tableView
dequeueReusableCellWithIdentifier:#"NearYouCell"];
SWHNearYou *aPointer = [self.thingsNearYou objectAtIndex:indexPath.row];
cell.customNearYouLabel.text = aPointer.restrauntsNearYou;
return cell;
}
I want to change the text of customNearYouLabel upon a button press but work out how to get a pointer to the cell in my -IBAction method.
Thanks
You can just handle that in your tableView:didSelectRowAtIndexPath: method by grabbing the cell.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.customNearYouLabel.text = #"New Text";
}
I presume the button is not on a table cell?
If so, you just need to update the relevant value in the self.thingsNearYou array.
If you then call [tableView reloadData] then the table will reload it's data and the text of customNearYouLabel will change.
Worked it out - needed to add in self before tableview
-(IBAction)cancel:(id)sender{
SWHNearYouCell *cell = (SWHNearYouCell *)[self.tableView
dequeueReusableCellWithIdentifier:#"NearYouCell"];
cell.customNearYouLabel.text = #"New Text";
NSLog(#"This is it: %#",cell.customNearYouLabel.text);
}
I'll need to spend some more time on it as [self.tableView reloadData]; will not update the table but I reckon that should be easier to solve.

Highlight table view cell on click, not on release

I have a table view with a list of items. When I click on one of those items I want the background to be highlighted. I have that code working but the colour changes on release, not on the click itself. How can I highlight when the user taps on the cell and not when he/she releases it?
Here is my code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// Navigation logic may go here. Create and push another view controller.
Help_Cell *cell =(Help_Cell*) [tableView cellForRowAtIndexPath:indexPath];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
UIView *v=[[UIView alloc] initWithFrame:cell.frame];
v.backgroundColor=[self colorForHex:[appDel.appColorSettings objectForKey:#"cellColor2"]];
cell.backgroundView=v;
cell.title.textColor=[self colorForHex:[appDel.appColorSettings objectForKey:#"cellColor1"]];
}
I want it to happen like a button. Is there an onClick method for a UITableView or a UITableViewCell besides didSelectRowAtIndexPath?
EDIT
Here is my code from cellForRowAtIndexPath
-(void)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
Help_Cell *cell =(Help_Cell*) [tableView cellForRowAtIndexPath:indexPath];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
UIView *v=[[UIView alloc] initWithFrame:cell.frame];
v.backgroundColor=[self colorForHex:[appDel.appColorSettings objectForKey:#"cellColor2"]];
cell.selectedBackgroundView=v;
}
At the time of creating cell, in cellForRowAtIndexPath, write below code:
UIView *v=[[UIView alloc] initWithFrame:cell.frame];
v.backgroundColor = [self colorForHex:[appDel.appColorSettings objectForKey:#"cellColor2"]];
cell.selectedBackgroundView = v;
No need to write anything in didSelectRow method.
You can consider this as option
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView selectRowAtIndexPath:indexPath.row animated:YES scrollPosition:UITableViewScrollPositionTop];
}

Table view with checkmark

Hi, Iam new to iPhone apps. I have a problem with tableview. Here I want to add checkmark when I click on the cell, it's working fine. But whenever I click on another cell then the before cell should not show the checkmark.Thanx in advance.
I wrote an example project for this a while ago ExclusiveCheckedTableView
A cell's accessory type is set by the UITableViewDataSource protocol. This code snippet toggles the cell's accessory type between 'none' and 'checkmark':
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"checkableTableViewCell";
OEListTableViewCell *cell = (OEListTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[OEListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.accessoryType = (cell.accessoryType == UITableViewCellAccessoryCheckmark) ? UITableViewCellAccessoryNone : UITableViewCellAccessoryCheckmark;
}

how to set accessoryType when cell has been selected

I have been trying to figure out how to set the accessoryType to UITableViewCellAccessoryCheckmark when the cell is selected but am having trouble finding a decent example of this.
If you know how to do this or a good tutorial could you please let me know that would be great.
To restrict the user to just one selection, meaning to create an exclusive list of one choice only, you could follow these steps;
Firstly, have a global index path declared in your .h file to keep track of the already selected cell ->
NSIndexPath *oldIndexPath;
When you create the cells, be sure to set the accessory type to none, so that no cell is selected by default when the table is seen;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"CallIdentifier"];
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
Finally, in the didSelectRowAtIndexPath delegate method, add the following code which will remove the checkmark from the already selected cell, and add a checkmark to the newly selected one.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (oldIndexPath==nil) { // No selection made yet
oldIndexPath=indexPath;
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
else {
UITableViewCell *formerSelectedcell = [tableView cellForRowAtIndexPath:oldIndexPath]; // finding the already selected cell
[formerSelectedcell setAccessoryType:UITableViewCellAccessoryNone];
[cell setAccessoryType:UITableViewCellAccessoryCheckmark]; // 'select' the new cell
oldIndexPath=indexPath;
}
}
Hope this works out! :)
Something like this may work:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self tableView:myTableView cellForRowAtIndexPath:indexPath];
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
To answer the comment below, just push a viewController in the same method like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self tableView:myTableView cellForRowAtIndexPath:indexPath];
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
// Then push a new view
iPhoneCustomViewController *myVC = [[iPhoneCustomViewController alloc] initWithNibName:#"iPhoneCustomViewController" bundle:nil];
[self.navigationController pushViewController:myVC animated:YES];
[myVC release];
// And deselect the row (if desired)
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Did you know that:
1.) UITableView keeps track of the index paths for the rows that have been selected? It's in an array called indexPathsForSelectedRows
2.) UITableView has a flag you can set to make it either single or multiple selection. You can change it by calling the setter setAllowsMultipleSelection:(BOOL).
So, assuming that the table has been set to single selection, we can do the following in the tableView:CellForRowAtIndexPath method ...
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[[cell textLabel] setText:#"Some Text"];
NSArray *selectedIndexPaths = [tableView indexPathsForSelectedRows];
if ([selectedIndexPaths containsObject:indexPath]) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}else{
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
return cell;}
This implementation of CellForRowAtIndexPath will give you a clean checkmark with no gray background when a cell is selected. You will need to set the checkmark in the didSelectRowAtIndexPath delegate method to make sure a cell gets the checkmark the moment it gets selected.
No need to create separate ivars or anything else to keep track of what was or wasn't selected. It's all neatly contained in the UITableView as Apple intended.
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType=UITableViewCellAccessoryCheckmark;
Implement this in didSelectRowAtIndexPath delegate method
From the docs:
The delegate handles selections in this method. One of the things it
can do is exclusively assign the check-mark image
(UITableViewCellAccessoryCheckmark) to one row in a section
(radio-list style). This method isn’t called when the editing property
of the table is set to YES (that is, the table view is in editing
mode). See "Managing Selections" in Table View Programming Guide for
iOS for further information (and code examples) related to this
method.
Here is an example:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]
cell.UITableViewAccessoryType = UITableViewCellAccessoryCheckmark;
}

Change UITableView Cell Image using IF Statement?

I want to add an image using the cell.imageView setImage: method. However, depending on field from an array (in the form of an NSString and called using [dog types]) I need the image at the left-hand side of the cell to change to an image which reflects the type of dog.
How do I go about doing this? Thanks.
As you know for fill the cells contents you have to use the delegate method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
so, you have to put your IF inside this method and use indexPath.row to understand witch row you are drawing.
Here a simple example:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"] autorelease];
}
UIImage *a,*b; //Remeber to init with your images:)
if(indexPath.row == 1)
{
[cell.imageView setImage:a];
}
else
{
[cell.imageView setImage:b];
}
return cell;
}
I hope it's what you was looking for :)
I think you can deffinatly can do wht you want to do.
By chacking condition in cellForRowAtIndexPath method :
if(indexPath.row == 0) // for first cell of tableView
if(indexPath.row == 1) // for second cell of tableView
hope you can understand...
even if any problem leave a comment...and mark as correct if you got resolved...