How to save tableView custom cells to mutablearray - ios

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.

Related

How get values from UItable View cell

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.

How to create object in Objective-C?

I am new in iOS development. Currently I am reading this tutorial http://www.appcoda.com/how-to-handle-row-selection-in-uitableview/ . I am facing problem when I am reading this line
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
I know object in objective-c is created by following way
classname *objecname = [[classname alloc]init];
My confusion point is here UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
How cell object is created here? Please tell details.
Based on the tutorial you linked to, I'm assuming you are dealing with selecting a row. When you select a row, you have access to the NSIndexPath of that row, which contains two parts:
The section of the cell
The row of the cell
With that information, let's break down the confusing code: UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UITableViewCell *cell
This part declares your variable. You've chosen to name it cell. You could just as easily have named it theCell, like so:
UITableViewCell *theCell
The * means that you're declaring a pointer, which is just a reference to an actual object, or the table view cell.
[tableView cellForRowAtIndexPath:indexPath]
The tableView refers to the UITableView that was just selected. UITableView has a method called cellForRowAtIndexPath, and what that method does is retrieve the cell at the specified section and row of the UITableView. In your case, it retrieves the row that you've just selected and stores the reference to it in your cell variable.
When declaring a new object, yes, it would take on the following syntax:
classname *objecname = [[classname alloc] init];
The key word here is new. When dealing with selecting rows in a UITableView, you don't want to create a new cell because you can't select a cell that doesn't exist. You want to get the cell that the user has just selected.
I assume you have this code in your didSelectRowAtIndexPath: method, is that right?
If so, you're tapping on a cell that is on the screen, so the cell object already exists. Where and how was it created? Inside cellForRowAtIndexPath:, which you have already written.
So when you make this call
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
it just gives you the cell object to work with, but it doesn't create a new one.
The cell is not created with that line. That line gets a reference to a cell that is already in the table view at that indexPath.

Making a reference to a custom UITableViewCell

I have the tag for a custom UITableViewCell but I am not sure how I should reference it?
What is the standard approach here.
I thought something close to this but of course this will not work right?:
CustomCell *cell = (CustomCell*)[self.mainTableView cellForRowAtIndexPath:[NSIndexPath indexPathForItem:tag inSection:0]];
I make this reference when a button is pressed in the custom implementation of that cell using a delegate method to pass it to my view controller.
You should not refer to cell itself, because is just interface to show your data. Cells can be (and should be) reused and filled with different data.
Better is store data id, or at lest index of element that cell.
Read more in docs A Closer Look at Table View Cells
To get from a button in a cell to the cell itself, walk up the view hierarchy:
UIView* v = sender; // sender is the button that was tapped
do {
v = v.superview;
} while (![v isKindOfClass: [UITableViewCell class]]);
DiscoverCell* cell = (DiscoverCell*)v;

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