How to disable a insert button for a Tableview Cell - iphone

I want to make a list so that users can add items to their favorites. I used uitableviewcelleditingstyleinsert for my tableview. When a user taps an insert button which has a + sign, the item will be added to the favorites list. However, I want each item in the favorites list to be unique, so I when a button is tapped, I want it to automatically become grayscale. How could I set this up in my app?

This is completely possible, but not the way you're going about it. You need to create your own accessoryView with the plus sign image, use - (void) accessoryButtonTapped: (UIControl *) button withEvent: (UIEvent *) event to know when it's been tapped and then change the UITableViewCell's accessory view based on the subsequent change to your data source.
//in your cell creation
UIImageView *i = nil;
if(cellAlreadyUsed)
i = [[UIImageView alloc] initWithImage:#"your_gray_image"];
else
i = [[UIImageView alloc] initWithImage:#"your_green_image"];
cell.accessoryView = i;
[i release];
- (void) accessoryButtonTapped: (UIControl *) button withEvent: (UIEvent *) event {
//handle changing your data source to reflect that cell was used and reload your table
}

If u want every item unique apply logic before inserting it to array.
Compare ur array items with the current item, if match with array item break the loop, u can show an alert also, if not add item to the array list.
It will be a tedious task to customize the insert button.

Related

UIPickerView multi selection on ios7

I need a picker view witch i can select multi values, like the "select input" in HTML :
In ios6, i did it with a custom view created in the UIPickerViewDelegate (pickerView:viewForRow:forComponent:reusingView:) and a UIButton in each row, but since ios7, the custom view don't received the touch event.
Is it possible to do it in ios7 ?
DrDisc has confirmed that it is not possible to handle a touch event directly from a row view since ios7.
But it is possible to :
add a Tap Gesture to the UIPickerView
retrive the selected view
call a method to check / uncheck the row
int row = [self.pickerView selectedRowInComponent:0];
UIView *rowView = [self.pickerView viewForRow:row forComponent:0];
if([rowView isKindOfClass:[YouCustomView class]])
{
[(YouCustomView*)rowView toggleCheck];
[self.pickerView reloadAllComponents];
}
I think it is more natural than a button to check/uncheck, but we lost the ability to select an other row with a tap on it.
As far as I know, this is not possible. One option is to utilize UIPickerView's delegate method:
- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
You could store the selection in an array and change the text passed through from the data source to some sort of selection text ("x Selection 1").
Alternatively, you could have a 'select item' button that would add the currently displayed value to the selected array.
When the user clicks a button you can then look through the selected array for those that were selected.
These may not be the best methods, they're just my initial thoughts on it.

accessing cell's properties in delegate method

I have a play button, which when pressed, plays a sound, and then changes the image to a pause button. The button is a property of my cell which is in my table view.
I'm able to access the cell when the user hits the button because I can do this:
UIButton *b = (UIButton *)sender;
myTableViewCell* cell = (myTableViewCell*)[[b superview] superview];
And then I can say
[cell.button setImage:myImage];
But I want to change the image of the button back to a play button once the sound is over. I know about the delegate method
-(void)audioPlayerDidFinishPlaying:
(AVAudioPlayer *)player successfully:(BOOL)flag
{
NSLog(#"audioPlayerDone");
}
And I do get the NSLog when it is completed, but how could I access the cell and change the image? I don't have the luxury of calling superview twice as I did before as there is no sender in this delegate method. Any help??
The table view represents some content (audio), therefore it makes sense you have a model (object) behind each table view cell.
For each cell you could create an object with 2 or more properties:
- audiofile: path to a file
- is playing: boolean value
The moment an audio file starts playing (button is pressed) you set the boolean value "is playing" to true and you update the table view so a different image is displayed on the button. To figure out which object you need to change state for, use the method -indexPathForCell:. The code will look somewhat like the following for your button press method:
UIButton *b = (UIButton *)sender;
myTableViewCell* cell = (myTableViewCell*)[[b superview] superview];
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
AudioObject *audio = [dataArray objectAtIndex:indexPath.row];
audio.isPlaying = true;
[tableView reloadData];
The moment the audio file stops playing you can check the array with models for any file that is playing and set it to false. Afterwards you update the table view again so the button image changes back to the default one.
An implementation like the one I described above will work fine if only 1 audio file is playing at any time.

How to implement custom cell selection behavior with button for UITableViewController?

I'm trying to replace standard table cell selection behavior with a button-press behavior. The result should be same - to push detailViewController on top and to present some detail info. The cell view is loaded from a separate xib file. So, I need to put a button in that cell xib and use that button for opening detail VC in table VC. I have placed the button view in cell xib, and disabled cell selection in table VC viewDidLoad, but now, I'm not sure how to handle that button press, where to place the button press code or what delegate method to use because didSelectRowAtIndexPath is not respoding (cos I disabled it). Any suggestions?
UPDATE: I need to get some info from cell (section, index) on button press in order to initialize the data for detail VC. Also, should note, that there will be several buttons in each cell.
UPDATE2: I'm using rows with sections, so on button press I need to pass both section and row numbers that corresponds to cell where the button was pressed.
UPDATE3: I have tried to use unique tags for identifying cells but it looks like it's not the solution because I'm getting problems with cell reusing. The first 9 rows are created and relevant tag number are set to them. But then every row from 10 is reused from the queue, so I get the same 9 view with their tags. For example, 10th row pops me the first row button from the queue with the tag 1, 11th - second row button with tag 2 and so on. I mean, the whole dequeue cells means that it doesn't matter how much rows do you have in table, there will be reused 9 cell views for all of the table rows. That means I cannot assign unique tags for the cells because the cell views are not unique for each row
If i got u r question right then this will be solution
[cell.yourButton addTarget:self action:#selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
//you can use tag property of button
NSUInteger tag=indexPath.row+1;
cell.yourButton.tag=tag;
this code should be in
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
& you write action as
-(IBAction)myAction:(id)sender{
UIButton *but=(UIButton *)sender;
NSLog(#"Tag-%d",but.tag);
NSUInteger index=but.tag-1;
//using tag you will come to know which button is pressed ...
}
The suggestions with tags didn't worked out because of a reason described in UPDATE3. However, I did succeed by calling "magic" indexPathForRowAtPoint: method. So the solutions is:
1) attach custom method for button press in cellForRowAtIndexPath:
// I have already created/loaded and got a handler to my button btn
// here comes attaching
[btn addTarget:self action:#selector(openDetailViewController:event:) forControlEvents:UIControlEventTouchUpInside];
2) Define custom method:
-(void)openDetailViewController:(id)sender event:(UIEvent *)event{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:currentTouchPosition];
// do my stuff
}
So, when the button gets pressed my method gets called, passing some additional parameter like event. By calling "allTouches" and "anyObject" I'm getting the touch. Then, I'm retrieving CGPoint from that touch, then passing that point to "indexPathForRowAtPoint" that returns desired indexPath with section and row number. Cool :)

Passing a value on a IBAction button method in a UITableview Row

I have a value called member id and I want to send it to another view controller, If I place the following in didSelectRowAtIndexPath, The value passes to the variable "member".
int memberIndex = [indexPath indexAtPosition: [indexPath length] - 1];
member = [[tableDataSource2 objectAtIndex: memberIndex] objectForKey:#"Memberid"];
If it is placed in the cellForRow, of course it rewrites with every row created. I have a button in each row that launches a viewController, I want the button action to grab the rows "member" and pass it to the new controller. Is there a "didSelectButton at index path method" or a way to grab that on the fly?
any Ideas would be great. It's the first time I'm adding a button to a UiTableview.
Thanks
Why not use the accessory view? It is a built-in button that you can skin with any image you want to give the UI any kind of feel that is required. Then add this to the table's delegate:
accessoryButtonTappedForRowWithIndexPath: (NSIndexPath *) indexPath{ ... }
You will then be able to call your method on your table data source and launch your secondary view.
Just play with "tag".
Each UIView has an attribute tag (int).
in the cellForRowAtIndexPath :
//create yourButton and then
yourButton.tag = memberIndex;
and when you are using an IBAction just get the sender :
- (IBAction) didSelectButton:(id)sender
{
int memberIndex = ((UIButton *)sender).tag;
//
}
tips : to get the sender when you are setting the action property of your button don't forget the ":"
E.G
action = #selector(didSelectButton:);

iPhone - How to determine in which cell a button was pressed in a custom UITableViewCell

I currently have a UITableView that is populated with a custom UITableViewCell that is in a separate nib. In the cell, there are two buttons that are wired to actions in the custom cell class. When I click one of the buttons, I can call the proper method, but I need to know which row the button was pressed in. The tag property for each button is coming as 0. I don't need to know when the entire cell is selected, just when a particular button is pressed, so I need to know which row it is so I can update the proper object.
Much easier solution is to define your button callback with (id)sender and use that to dig out the table row index. Here's some sample code:
- (IBAction)buttonWasPressed:(id)sender
{
NSIndexPath *indexPath =
[self.myTableView
indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
NSUInteger row = indexPath.row;
// Do something with row index
}
Most likely you used that same row index to create/fill the cell, so it should be trivial to identify what your button should now do. No need to play with tags and try to keep them in order!
Clarification: if you currently use -(IBAction)buttonWasPressed; just redefine it as -(IBAction)buttonWasPressed:(id)sender; The additional callback argument is there, no need to do anything extra to get it. Also remember to reconnect your button to new callback in Interface Builder!
You could use the tag property on the button to specify which row the button was created in, if you're not using tags for anything else.
For a implementation that is not dependent on tags or the view hierarchy do the following
- (void)btnPressed:(id)sender event:(id)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchPoint = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:touchPoint];
}
I have the same scenario. To achieve this, I derived a custom cell. I added two properties, section and row. I also added an owner, which would be my derived TableViewController class. When the cells are being asked for, I set the section/row based on the indexPath, along with the owner.
cell.section = indexPath.section
cell.row = indexPath.row
cell.owner = self
The next thing that I did was when I created the buttons, I associate the button events with the cell rather than with the tableViewController. The event handler can read the section and row entry and send the appropriate message (or event) to the TableViewController. This greatly simplifies house keeping and maintenance by leveraging existing methods and housekeeping and keeping the cell as self contained as possible. Since the system keeps track of cells already, why do it twice!
Even easier:
-(IBAction) buttonPressed {
NSIndexPath *myIndexPath = [(UITableView *)self.superview indexPathForCell: self];
// do whatever you need to do with the information
}
Here's a Swift example...
The UIControl class is the superclass of various iOS widgets, including UIButton, because UIControl provides the target/action mechanism that sends out the event notifications. Therefore a generic way to handle this is as follows:
func actionHandler(control: UIControl)
var indexPath = tableView.indexPathForCell(control.superview!.superview! as UITableViewCell)!
var row = indexPath.row
}
Here's an example of setting up a button control to deliver the action. Alternatively, create an #IBAction and create the action visually with Interface Builder.
button.addTarget(self, action: "actionHandler:", forControlEvents: .TouchUpInside)
You can downcast UIControl parameter to UIButton, UIStepper, etc... as necessary. For example:
var button = control as UIButton
The superview of control is the UITableViewCell's contentView, whose subviews are the UIViews displayed in the cell (UIControl is a subclass of UIView). The superview of the content cell is the UITableViewCell itself. That's why this is a reliable mechanism and the superviews can be traversed with impunity.
You can access the buttons superview to get the UITableViewCell that contains your button, but if you just need the row number, you can use the tag property like the previous post deacribes.
There are multiple methods to fix the problem.
You can use the "tag" property
Give the value indexPath.row as the tag value for the button.
btn.tag = indexPath.row;
Then in the button function, you can easily access the tag value and it will be the index for the clicked button.
-(void)btnClicked:(id)sender
{
int index = [sender tag];
}
You can use the layer property
Add the indexPath as the value in the layer dictionary.
[[btn layer] setValue:indexPath forKey:#"indexPath"];
This indexPath is accessible from the button action function.
-(void)btnClicked:(id)sender
{
NSIndexPath *indexPath = [[sender layer] valueForKey:#"indexPath"];
int index = indexPath.row;
}
With this method you can pass multiple values to the button function just by adding new objects in the dictionary with different keys.
[btnFavroite setAccessibilityValue:[NSString stringWithFormat:#"%d",indexPath.row]];
[btnFavroite setAccessibilityLabel:btnFavroite.titleLabel.text];
[btnFavroite addTarget:self action:#selector(btnFavClick:) forControlEvents:UIControlEventTouchUpInside];
-(void)btnFavClick:(id)sender{
UIButton *btn=(UIButton *)sender;
int index=[btn.accessibilityValue integerValue]]
}
this solution also works in IBAction connected using storyboard cell prototype
- (IBAction)viewMapPostsMarker:(UIButton*)sender{
// button > cellContentView > cellScrollView > cell
UITableViewCell *cell = (UITableViewCell *) sender.superview.superview.superview;
NSIndexPath *index = [self.mapPostsView indexPathForCell:cell];
NSLog(#" cell at index %d",index.row);
}