Send cell string value with button action - ios

Hello I have working json parsing with custom cell and i have some label values and i added buttons for every cell and i want to take ID when clicked button on cell. My codes here
In My Tableview codes
cell.IDLabel.text= ID;
cell.cellButton1.tag=200+indexPath.row;
Button Action
- (IBAction)didTapCellButton1:(UIButton *)sender {
int *myrow = sender.tag-199; // Now i know which row clicked with this code , But i want to know IDLabel value when this button clicked
}
i know which row clicked with didTapCellButton1 action code , But i want to send IDLabel value when this button clicked
Thank you !

You should create #protocol for your custom cell, something like - (void)myCustomCellDidSelectButton:(MyCustomCell *)cell, than implement delegate in your ViewController and set cell.delegate = self in cellForRowAtIndexPath:. Now when your button's action fires inside cell call delegate method. When you have cell you can check your ID even without your DataSource.

finally . I found easy way :)
My working codes here;
cell.IDLabel.text= ID;
int valueid = [cell.IDLabel.text intValue];
cell.cellButton1.tag=valueid;
And when button clicked
int myvalue = sender.tag;
I think will be help too many people
thanks !

Related

Two clickable items in UITableView

I have a UITableview as a contact list in which there are a lot of users. It has a thumbnail photo and profile details on each row. I want to make it like when clicking on thumbnail, it goes another page for photo and when clicking on the rest of the space it goes to somewhere else. By using table view delegate I know which row is clicked and pass data, like user id to a new ViewController. But can I know which row when the thumbnail is clicked?
I am using the tag to find the view from cell, like
UIImageView *thumbnailView = (UIImageView *) [cell viewWithTag:1];
I think I cannot label the row index by tag.
You can add gesture to thumbnail image and get events on it. Need to set tag for thumb image as per indexPath.row.
Add following code in you in cell datasource method (cellForRowIndexPath:) :
cell.YOURIMGVIEW.userInteractionEnabled = YES;
cell.YOURIMGVIEW.tag = indexPath.row;
UITapGestureRecognizer *clickable = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageClicked:)];
clickable.numberOfTapsRequired = 1;
[cell.YOURIMGVIEW addGestureRecognizer:clickable];
[clickable release];
And also used below method :
-(void)imageClicked:(id)sender
{
UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;
NSLog(#"image tag is = %d", gesture.view.tag);
////////
You custome come goes Here !!!!!!!!!!!!!!
///////
}
You can use the photo as accessory view. Or use a UIButton with the photograph as background and set the action accordingly. (e.g. call a method on the view controller which then performs the push or segue. Doing so you will have to pass some data to the view controller indicating which row was actually clicked in. The number of the row can be used or you can set the tag of the button with some numeric id.)
Go the Easy way because doing it hard-way won't grant you a president award, right ?
Instead of UIImageView take a UIButton and set the Image on UIButton instance.
Set Button tag as the indexPath.row so that when you retrieve it you know which row is clicked. You can also sort it the other way but it seems quiet handy.
Add target into the button to a custom function. [ btnObj addTarget ...... ]
tyepcast you sender to a UIButton and receive the tag (indexpath.row)
You can remove all gesture recognizers and buttons in tableviewcell, and in your controller, you can implement below delegate;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
To make this method triggered, you need to set tableview delegate as your controller, either via code as below, or using storyboard.
- (void)viewDidLoad {
self.tableview.delegate = self;
}
By doing this, it won't matter which item you clicked in your cell, delegate method will be called always.
I hope this helps.

Custom UITableViewCell button click

In a UIViewController there is a UITableView which consists custom UITableViewCell with a UIButton.
When the user clicks the button (in the custom UITableViewCell), I want to display a message in the hosting UIViewController - what is the right way to achieve that? How should I notify the UIViewController that the button was clicked?
You should use delegation to tell the UIViewController that something happened in the cell.
I'm not exactly sure what kind of message you are referring to but if you mean a you want to show a popup just create a UIAlertView and show it when the button is clicked in the cell.
However, if you mean change a UIlabel text or whatever as a message on the ViewController you can always pass whatever you need to change into the TableView and Cell or the UIViewController itself into it.
I usually create a method on the custom cells class and pass it whatever I need into the custom TableView's constructor. Then in the TableView's GetCell() method pass whatever I need to change into that method I created on the cell and save it as a variable on the cell.
If you've initiated your UITableViewCell, and added a button to the cell, you can use a Delegate when the button is clicked, something like this:
UIButton button = new UIButton (cell.Bounds.X, cell.Bounds.y, cell.Bounds.Width, cell.Bounds.Height);
button.TouchUpInside += (object sender, EventArgs e) =>
{
label.text = "Blablabla";
};
Where the label is, is up to you. I hope this helps, good luck!

how to create tabs that expands when selected in iOS

I want to create a tableView or tabs that expands when user selects them. This is very commonly used in webpages using jquery.
you can check http://jqueryui.com/accordion/ It does exactly what i want to do in my ipad app. with horizontal tabs too http://jqueryui.com/tabs/
Can anyone tell me how to achieve this in my iPad app ? Or any pointers would be appreciated.
TIA
Sam
1.Use UITableView for first type of tab.
a) Use Header and Cell View in your desired format.
b) Hide and unhide cell view with some animation.
2.Use UISegementedControl for second type of tab.
a) Use function addTarget :
[self.mySegmentedControl addTarget:self action:#selector(segmentChanged) forControlEvents:UIControlEventValueChanged];
b) Implement segmentChanged :
- (void) segmentChanged:(UISegmentedControl *)paramSender{
//check if its the same control that triggered the change event
if ([paramSender isEqual:self.mySegmentedControl]){
//get index position for the selected control
NSInteger selectedIndex = [paramSender selectedSegmentIndex];
if (selectedIndex == 1 ) { // do required } and so on....
}
}
The best possible way is using the headerview to show the Master row and the tableview cells for showing the Details row.Write a touch event for the headerview and set it with the showing and hiding the cells ,with neat row animation.You need to keep the status of the opened and closed state of cell with an array of bools repesenting the no of masterview.

Handle button click inside UITableViewCell

I can't seem to handle a button click properly inside a custom UITableViewCell. It's a cell that contains a label and a button. I have the following code:
var cell = tableView.DequeueReusableCell (cellKey) as ApptHistoryCell;
if (cell == null)
{
cell = _container.Cell;
cell.SelectionStyle = UITableViewCellSelectionStyle.None;
}
if (InfoClicked != null)
{
cell.ActionButton.TouchUpInside += InfoClicked;
}
InfoClicked is an event handler passed on in loop on cell creation. When the cell is re-used, this causes a null reference exception, because (I think) TouchUpInside is trying to call 2 handlers. Old one and the new one, which causes a crash. If I put the event handler inside cell == null if block, then the wrong action is shown.
How can I handle the click properly?
Thanks!
The way I handle buttons inside custom cells:
I define an IBAction that I connect to a UIButton event inside the custom cell
I define a delegate and a delegate method for the cell to handle the button action
The IBAction calls that delegate method
When defining your cells in cellAtRow... I set the tableViewController to be the delegate. cell.delegate=self;
I do whatever action I would like to do inside that delegate method in the tableViewController
Makes sense?
you should create your cell from a xib file, which already connects the buttons to the owner's targets (the view controller).
In your custom cell just locate your button at IB and make connection with the property.
Now, in the controller at cellForRowAtIndexPath, what you need, just tag that button, say:
(MyCustomCell*)cell.myButton = indexPath.row;
Now, after that just set the click event like that:-
[(MyCustomCell*)cell.myButton addTarget:self action:#selector(NeedFunctionToCall:) forControlEvents:UIControlEventTouchUpInside];
Now, at same view controller, implement that required method, like that:
-(void)NeedFunctionToCall:(id)sender
{
NSLog("Got the event :-> %d, from the cell/button",[sender tag]);
}

Custom UITableViewCell Button and RowSelected (iOS/Monotouch)

I have a 'Load More' UIButton inside a custom UITableViewCell. My table also uses a Delegate. I want to reload the table data when this button is clicked. I have noticed that when I click the button, nothing happens. But when I click on the area around the button i.e. the actual Cell, the Delegate gets the RowSelected event.
I tried using SetSelected(true,false); from the button's touch up inside event to see if it would fire the RowSelected event for the delegate, but it didn't.
Can anyone explain me how I can make a button click for a Custom Table cell fire the actual Row Selected event? If this isn't a viable option, is there a better way to 'Load more data' other than placing the button inside a custom table view cell?
Thanks!
UPDATE:
I do use ContentView.AddSubView as shown in the code below. Please note I'm using Monotouch, but the logic is the same:
public LoadMoreCell (string reuseCellIdentifier)
:base(UITableViewCellStyle.Default, reuseCellIdentifier)
{
SelectionStyle = UITableViewCellSelectionStyle.None;
_btnLoadMore = UIButton.FromType(UIButtonType.RoundedRect);
_btnLoadMore.Frame = new RectangleF(10,5,300,30);
_btnLoadMore.SetTitle("Load More", UIControlState.Normal);
_btnLoadMore.UserInteractionEnabled = true;
ContentView.AddSubview(_btnLoadMore);
}
I have a TableDelegate that has a RowSelected method where I reload the data...however, the RowSelected only gets fired when I click the actual row.
I think you might have added the uibutton as
[self addSubView:yourButton];
instead use :-
[self.contentView addSubview:yourButton];
and all the other controls of your custom cell.
Do this in the initializer of your custom cell :-
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {}
make a custom UITableViewDataSource for your table
and when you hit the button 'Load more', call a method of this datasource to add other data to the list of the datasource, add this datasource to your table and make sure to reload the data of the table
this.tblData.ReloadData ();

Resources