There is CustomCell in tableviewcontroller .And in which there is dynamic txtfield. which is viewed by tag value. I am saving textfield value on dictionary on "textFieldShouldEndEditing". I want to save textfield value on button (Textfied is dynamic,no outlet is created for textfield).
First Image attached
I want to edit the saved address. Second Image attached
kindly revert asap
If i understood your question properly then,
You need to set the button (Which i guess is also inside the custom cell), same tag as that of the indexpath of the cell.
By that you can get the indexPath of the cell and get the cell by
MyCustomCell *cell = [self.tableview cellForRowAtIndexPath:button.tag]
Then, you can get the textfield inside the cell also by just calling cell.textFieldName.text, and you will get the text.
For more clarity the IBAction method of the button, should be something like this.
-(IBAction)buttonClickedToSaveTexd:(id)sender{
UIButton *btnOfCertainCell = (UIButton*)sender;
MyCustomCellClass *currentCell = [self.tableView cellForRowAtIndexPath:btnOfCertainCell.tag];
NSString *textOfTheCell = currentCell.textFieldName.text;
}
Again, for this to work, in cellForRowAtIndexPath you need to assign the button tag as indexPath.row. You dont need to set tag of the textfield.
Related
I am looking for is a clean way of changing the model after tapping a button in a custom cell. It's tapping the button that matters here and not selecting the row, so didSelectRowAtIndexPath isn't what I am interested in.
For example
let array = [true, false, true]
When the user taps the button in the second cell, the model should become
array[1] = true
In cellForRowAtIndexPath a target action is added to the button :
cell.button.addTarget(self, action: "myAction:", forControlEvents: UIControlEvents.TouchUpInside)
the action looks like :
func myAction(sender:UIButton!)
{
//how to change the model and reloadRowsAtIndexPaths since the sender is just a UIButton!?
}
If you only have one section in the tableViewController, you could always set button.tag = [indexPath row] inside cellForRowAtIndexPath. Then your action would always know which row was touched from the sender.tag value. Quick and dirty, but it would work.
Or, if you will reuse this custom cell in tableviews with multiple sections, you could subclass the UIButton and add a property for the indexPath. Then you would always have access to both section and row values. Personally, I would take this approach because it gives you the most flexibility in the long run.
inside of cellForRowAtIndexPath()
cell.button.tag = indexPath.row
Then inside of your function myAction()
func myAction(sender:UIButton)
{
var index = sender.view.tag
array[index] = true // just like you want
}
I am a little unclear about your question - are you asking how you can change a uitableviewcell's content once a button is pressed in the cell then one way to go about it would be to tag the button with the cell's indexPath when the cell is created in the cellForRowAtIndexPath method. Then you can read the tag from the selector once the button is pushed.
I just tested this and it works. When you create the cell add the line:
cell.tag=indexPath.row;
and then in the selector method for the button you can use:
NSInteger p=sender.tag;
int x=(int)p;
NSIndexPath *path = [NSIndexPath indexPathForRow:x inSection:0];
UITableViewCell *cell = [_tableView cellForRowAtIndexPath:path];
cell.backgroundColor=[UIColor blackColor];
In my example I changed the background color of the cell that the pressed button was in.
I have a prototype cell which with a button and a label in it. The text of the label changes for each cell. I have set up an #IBAction for the button and would like to get the text for the label in the cell in which the button in pressed. Does anyone know how to do this?
In didSelectRowAtIndexPath method you can get selected cell using
cellForRowAtIndexPath using indexPath.row
when you get selected cell object you can access that label which present inside the cell.
I hope it will help you.
- (IBAction)buttonHandler:(id)sender{
UIButton *button = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell *)button.superview;
println("%#",cell.textLabel.text)
}
I have a TableView with custom cell with a CheckBox, A label and a TextField. I want to get label text and TextField text of all the cells that I have CheckBox checked. How can I achieve it. whenever I scroll the TableViewCell index changed so I am not able to use rowAtIndexPath.
From my point of view, you will have to use array to keep track of which cells are checked. So declare NSMutableArray *checkedCells; variable and initialize it. In your didSelectRowAtIndexPath write following lines :
UITableViewCell *cell= [tableView cellForRowAtIndexPath:indexPath];
if (cell.radioChecked)
{
/*Create a object with textField and label value here.and add that object in checkedCells here*/
}
else
{
/*remove regarding cell object from checkedCells here*/
}
This way you will get properties of cells which are checked.
I have a custom TableView cell, and in that there's a button.
The table view have many rows used the same custom cell.
Now if the button of one of the cells pressed. I want to know which row of cell the button is in?
(1). In this method cellForRowAtIndexPath: , assign button with a tag.
For example:
cell.yourbutton.tag = indexPath.row;
(2). Add action for you button with same selector
[cell.yourbutton addTarget:self action:#selector(cellButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
(3). Then
-(void)cellButtonClicked:(UIButton*)sender
{
NSIndexPath *path = [NSIndexPath indexPathForRow:sender.tag inSection:0];
}
You can define your custom property and you can use it like below:-
#define kCustomProperty #"CustomProperty"
Associate your object with that custom property like below
objc_setAssociatedObject(yourButton,kCustomProperty , yourCellIndexPath, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
Get your data using the same property and object like below
NSIndexPath *yourCellIndexPath = (NSIndexPath *)objc_getAssociatedObject(yourButton, kCustomProperty);
Its a kind of custom property you can create by coding if you don't want to use tag.
Is there a way to get or set the textLabel of a standard UITableViewCell inside a custom method? For instance of a pseudo code,
-(void)getTextLabelOfUITableViewCell
{
UILabel *tempLabel = [[UITableViewCell section:0 row:1] textLabel];
}
-(void)setTextLabelOfUITableViewCell:(UILabel *)data
{
[[UITableViewCell section:0 row:1] textLabel] = data;
}
I'm trying to bind this with the PickerView delegate methods so whenever I change values in the datepicker or picker, the selected UITableViewCell will reflect the changes.
Otherwise I'd have to create a custom UITableViewCell. It'd be nice to be able to programmatically use the standard UITableViewCell Styles.
Thanks in advance!
You should keep (or pass) a reference to the "currently selected cell". Then you can easily access its text label by simply writing:
// assuming the selected cell is kept in a property by name selectedTableViewCell
UILabel * tempLabel = self.selectedTableViewCell.textLabel;
In case you're wondering how to keep such a reference, you should implement the
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
method, and in it, simply state:
self.selectedTableViewCell = [tableView cellForRowAtIndexPath:indexPath];
This way, when the user taps the cell, it will be set as the "selected cell".
Another way to fly would be by adding the reference to the cell you want to edit the text from in the IBAction as a parameter, like this:
-(IBAction)getTextLabelOfUITableViewCell:(UITableViewCell *)editMe;
And then you could write something like:
UILabel * tempLabel = editMe.textLabel;
You pretty much can do whatever you want with the label from there on.
Hope this helps!