How get values from UItable View cell - ios

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.

Related

saving dynamic textfield value on click of button

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.

displaying checkmarks for the selected cells after adding new cell - Swift

I have a UITableView with UITableViewCell's and once i select a cell it will display a checkmark for that cell until here its fine, but once i press the add button to add a new cell to that UITableView it will take me to another ViewController to write the text of the new cell and add it, so after adding it it will take me to the previous UITableView but without displaying the checkmarks to the cells that i have already selected before adding the new cell, is there a way to display checkmarks for the selected cells after pressing the back button from the other controller ?
Note: I am using Xcode with swift language to build my application.
The cells in your tableView is made with a array of some Object. In that Object you need to add a variable isChecked, and if it true, the tableviewcell at that indexpath will be checked.
you can do so by adding something like this in cellforrowatindexpath
if objectAtIndexPath.isChecked {
cell.accessoryType = .Checkmark
} else {
cell.accessoryType = .None
}
So the key is to have a have a variable in the object you are using to populate the viewcontroller.
When you create the new item, you simple say isChecked = true, and then add it to the array which contains the items, and it will be checked automatically.

How to save tableView custom cells to mutablearray

I have a tableView with custom cells.
Tableview is empty.
I have "+" button which add my custom cell with label and textfield
Question: how can i save to mutable array textfield.text of all tableview cells when user pressed "+" button
Code of a "+" button here...
- (IBAction)addButtonPress:(UIBarButtonItem *)sender {
MYCustomTableViewCell *nextCell =[self.myTableViewProperty dequeueReusableCellWithIdentifier:cellId];
if (!nextCell) {
nextCell = [[MYCustomTableViewCell alloc] init];
}
MYCustomTableViewCell *previousCell =[self.myTableViewProperty dequeueReusableCellWithIdentifier:cellId];
NSIndexPath *saveTextIndex = [NSIndexPath indexPathForItem:myCustomCellCount inSection:1];
// -- i can't get my customCell.textField.text
previousCell = [self.createTableView cellForRowAtIndexPath:saveTextIndex];
NSLog(#"%#", previousCell.textField.text); ////- its null=(
[cellArray addObject:nextCell];
[self.myTableViewProperty reloadData];
}
Add tag property to your textField that equals to you indexPath then you can simply access your textfields by their tag property.
Example:
Creating your new customCell
cell.textField.tag = indexPath.row;
Getting your cell's textField
MYCustomTableViewCell *previousCell =[self.myTableViewProperty cellForRowAtIndexPath:indexPath];
UITextField *txtF = (UITextField*)[previousCell viewWithTag:myCustomCellCount];
Please, have a look at the documentation UITableViewDataSource Protocol Reference before beginning. This link might be usefult as well. UITableView is just to display the content of your model (called datasource). So when addButtonPress: is called you should insert a data object into your datasource (e.g. an array) and tell the table view to reload it's content. In almost all cases dequeueReusableCellWithIdentifier: is ONLY called in tableView: cellForRowAtIndexPath:. That's where a new table view cell representing your datasource's data objects is created. And there's no need to store the table view's cells in an array because that's done by the table view itself. When the user is done pressing the button you just iterate over your datasource array and read it's data objects.

How to select multiple cells in a table view

I have a table view with multiple cells. When I click on one of the cell it navigates to the next view controller, but my problem is that I cannot select multiple cells.
Could any one please help me on this issue, I want to select two or more cells with two finger tap.
set by code if you set by programming
table.allowsMultipleSelection = YES;
and if you set in xib tick on allowsMultipleSelection
Just add button like checkbox and multiple choose like checkbox.
If you are implementing the tableview programatically then the better way is to create a custom cell which will have button (and obviously other UI components that you need to show in the table view cell) which will work as a checkbox and assign button.tag as indexPath.row which will help you to select multiple rows.
Take a look here --
Below code will go into cellForRowAtIndexPath
YourCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.button.tag = indexPath.row;
[cell.button addTarget:self action:#selector(multipleCheckAction:) forControlEvents:UIControlEventTouchUpInside];
return cell;
-(void)multipleCheckAction:(UIButton *)sender { //sender.tag will be equal to indexPath.row }
Happy Coding
Since cells are being reused, you need to set the accessory mark to on or off for every cell in the table in the cellForRowAtIndexPath table datasource method.
So the cell.accessoryType cell property should be specified in the cellForRowAtIndexPath and not the didSelectRowAtIndexPath delegate method.
In the didSelectRow, just keep track of the selected rows in an array, and set the cells accessory mark to none or checkmark in the cellForRowAtIndexPath depending on the array value.

How to alter tableview cell text?

I have a UITableView that I want to alter some of the static cells after I do other processing. I have outlets set up for the cells that I want to modify, but when I look at them using NSLog, they show nil, which indicates to me that I don't have the correct cell. For instance, in the image below I want to add the start time to the label just like I did for Date (date was done when creating the cells for which I got the current date),
I tap on the disclosure indicator which takes me to another scene (this was created in Storyboard, using segues to get from one scene to another) where I get the two times I need. I then return to the main scene (shown) and try to alter the Start Time label, but nothing happens. A NSLog of the label prior to trying to alter it returns this:
oStartTimeCell.textLabel.text: (null)
I have read in one of the Apple docs that this textfield is read-only. If that is true in this case, is there a way I can reload the cells with the updated information? Or is there another way to do this?
You're using the wrong approach. You should not create a reference to a cell using an outlet. Once the cell moves out of the visible view, the outlet will either be null or contain garbage data. Even if (in your situation) the cell will never move out of view, I think it shows you're trying to use a UITableView in a way that was not meant to be.
Instead put the data you want to display in your cells in a dataSource, e.g. an array.
The tableView should use the dataSource to configure the values displayed in the textLabels of the cells. Once you want to update the text displayed in the cells, change the values in the dataSource and call reloadData on the tableView to force the tableView to call -tableView:cellForRowAtIndexPath: and related UITableViewDataSource methods.
Try to create an IBOutlet for each cell and connect it:
IBOutlet UITableViewCell *cell1;
IBOutlet UITableViewCell *cell2;
IBOutlet UITableViewCell *cell3;
And also change your method to:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(indexPath.row == 0) return cell1;
if(indexPath.row == 1) return cell2;
if(indexPath.row == 2) return cell3;
if (cell == nil) {
//create cell;
}
return cell;
}
Are you using a UILabel to display the text ? . If you are just create an outlet to the UIlabel and update it any method like cellForRwoAtIndexPath or didSelectRowAtIndexPath etc that is called after you tableView is loaded.
If you are not using a UILabel and just using cell.textLabel you could do something like
cell.textLabel.text = #"ChangedText" in cellForRowAtIndexPathMethod. Make sure you are editing the required cell by checking indexPath.row
Do [tableView reloadData] to call cellForRowAtIndexPath.

Resources