Approaches to implement tapped inserting table cell with radio buttons - ios

What I'm trying to do is to insert some new data into the data source array, and then do [tableView reloadData]. The new array can be inserted, but there are two problems.
1) I want to have two radio buttons in the new cell for users to choose. Should I define a new cell object or something?
2) When reloading the data, the selected cell's color cannot be set to green as before.
Or any other suggestions on how to best implement this, thanks!!:

For the first problem, what I would do is pre-layout that cell containing the two radio buttons either in the storyboard (You can also do it in a .xib file). Then I will set an identifier for it like "LanguageSkillSelectionTableViewCell" or something. After that, when I am about to load the new set of data into the table view I can manage the presentation of cells in the table view through the callback cellForRowAtIndexPath:
An example would be if I want to load that LanguageSkillSelectionTableViewCell always at the beginning, then I would just set it at indexPath.row == 0. else set the contents of the data list to other rows.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *tableCell;
if(_loadedNewData)
{
if(indexPath.row < dataList.count && indexPath.row > 0)
{
tableCell = (DataTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"DataTableViewCell"];
//set attributes of the tableCell
}
else
{
tableCell = (LanguageSkillSelectionTablViewCell *)[tableView dequeueReusableCellWithIdentifier:#"LanguageSkillSelectionTableViewCell"];
//set attributes of the tableCell
}
}
else
{
//load in default order
}
return tableCell;
}
You can just play with the arrangement and presentation of the cells under this callback.
For the second problem, you have to store a flag or state for that selection picked by the user in a global variable within the class. Then everytime the cells are reloaded you can just update the selection state of that cell by setting a method for that cell that would update the selection state of the radio button.
For example:
If user selected English, then it would be like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *tableCell;
if(_loadedNewData)
{
if(indexPath.row < dataList.count && indexPath.row > 0)
{
tableCell = (DataTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"DataTableViewCell"];
//set attributes of the tableCell
}
else
{
tableCell = (LanguageSkillSelectionTablViewCell *)[tableView dequeueReusableCellWithIdentifier:#"LanguageSkillSelectionTableViewCell"];
//set attributes of the tableCell
[(LanguageSkillSelectionTablViewCell *)tableCell setLanguageSelectionState:canReadWrite];
}
}
else
{
//load in default order
}
return tableCell;
}

Related

Get UItextFields text from custom UITableView

I have a custom UITableViewCell with a UITextField (which is linked to the custom cells class). I am trying to access the textField from my VC class.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
menuCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
if ([indexpath row] == 2) {
menuCell.nameTextField.delegate = self;
}
return cell;
}
-(void) textFieldDidEndEditing:(UITextField*) textfield
{
}
How do I get the textFields text from textFieldDidEndEditing?
Depending on where you want to access this text depends on how difficult it is.
Want to access the text in cellForRowAtIndex - (very easy)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
menuCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
if ([indexpath row] == 2) {
menuCell.nameTextField.delegate = self;
}
NSString * text = menuCell.nameTextField.text;
return cell;
If you want to access the text anywhere in the VC and the menuCell is unique (there is only one of them) - (medium difficult)
In your header file add the custom cell as a class
#class menuCell;
This means you can set it a variable in the interface
menuCell * _menuCell;
Next in cellForRowAtIndex you want to allocate this custom cell
- (UITableViewCell *)tableView:(UITableView *)tableView_ cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == bCustomCellSection) {
if (!_menuCell) {
_menuCell = [tableView_ dequeueReusableCellWithIdentifier:bProfileNameCell];
_menuCell.nameTextField.delegate = self;
}
_menuCell.nameTextField.placeholder = #"Name";
_menuCell.selectionStyle = UITableViewCellSelectionStyleNone;
return _menuCell;
}
...
}
This means that we now have access to the menu cell from anywhere in the VC and can get the text by calling
_menuCell.nameTextField.text
Multiple custom cells with multiple textfields - (tough)
I have never done this but would probably do it one of two ways
a) Create an array and as we are creating the custom cells add a pointer to the textFields to the array each time. We can then access the textField we want from that array
For this method I would add the custom cells to a mutable array defined in the interface
NSMutableArray * cellsArray;
remember to initialise it in viewDidLoad
cellsArray = [NSMutableArray new];
Then in cellForRowAtIndex i would add the cell each time
menuCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
[cellsArray addObject: menuCell];
This obviously depends on how many sections we have. If we have more than one section it gets more complicated again:
Then we would need to add an array for each section to an overall array. This is quite complicated and could have a whole question on its own, there is a good link of how to do this here:
Once you have an array of cells (or an array of arrays of cells) you can call the cell you want based on the indexPath and get the textField
b) Call a pointer to the specific cell we want
menuCell * menuCell = [self tableView:table cellForRowAtIndexPath:indexPath];
and then get the textField from this cell as we did previously.
Remember you can calculate your own indexPath if you want to create one outside of cellForRow:
NSIndexPath * indexPath = [self.tableView indexPathForCell:cell];
This method is pretty good if you want to access a specific cell but a bit cumbersome if you want to access it a lot and keep having to call this code all over your VC
Hope this helps
If you are asking how to get the text from the delegate method textFieldDidEndEditing, then you simply do this:
-(void) textFieldDidEndEditing:(UITextField*) textfield
{
NSString *textFieldText = textfield.text;
}
However, if you have multiple textFields and you want to know what textfield is calling the delegate, you could tag your textField:
[myTextField setTag:indexPath.row]
and then put a if statement in the delegate textFieldDidEndEditing like this:
-(void) textFieldDidEndEditing:(UITextField*) textfield
{
if(textfield.tag == index0) do something..
else if(textfield.tag == index1) do something..
}

Display and hide DatePicker on UITableViewCell tap

I have a table view controller in which I need to display a date&time picker when a cell is tapped and hide it when the cell is tapped again. Basically the same effect that the iphone has when you choose your start and end date and time to create a new event in the calendar.
I'm guessing the display and hiding goes in the following method but I'm unsure of what goes inside:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {}
Some example code or link would be great. Thank you!!
Create your cell with whatever you want to show and the picker:
-----------------------------------
cell visible part
-----------------------------------
cell invisible part (with picker)
-----------------------------------
Define a property that let you know if you have to show the entire cell:
#property (nonatomic) BOOL shouldShowPicker;
Initialise this property (on viewDidLoad for example);
self.shouldShowPicker = NO;
A couple of methods to touch:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == 4) { //where your picker row is
self.shouldShowPicker = YES;
[self.tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 4 && self.shouldShowPicker) { //where your picker row is
return CELL_VISIBLE_PLUS_INVISIBLE_PART;
} else if(indexPath.row == 4 && !self.shouldShowPicker) {
return return CELL_VISIBLE_PART;
} else {
return OTHER_CELLS_HEIGHT;
}
}
You might find my answer here useful which describes what you need to do.
Inline UIPicker Implementation
Essentially you create a custom cell containing a date picker, with optional buttons. You then add this cell below the cell when you edit it and remove it when finished. All explained in the link.

UITableView - insert new row by tapping on existing cell

I use UITableView with prototype cells and would like the top most cell to be an "adder" cell: when user taps on that cell, the segue is being performed. As the UITableView is being dynamically generated (except the top one) and cells are being reused, I have two question:
1 - is it possible to make one static cell while all the others will remain prototype cells?
2 - If not, how to implement this solution? I think, it has to be implemented in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
but I have no idea, is this method right place to do it. Am I correct, to implement this custom cell, I need to check, whether indexPath.row = 0 and substitute the needed cell with custom, while programmatically shifting all the cells one row down?
Yiu can do on the Tap of Your cell, you can create two different cell, one your prototype cell and one for your details
in your
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
AdderCell *cell = (AdderCell*)[tableView dequeueReusableCellWithIdentifier:#"AdderCell" forIndexPath:indexPath];
} else {
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:#"CustomCell" forIndexPath:indexPath];
}
}
on tap of your Adder cell in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
// add data to your row array and reload the tableRow
} else {
// do your tap action
}
}

UITableView won't uncheck the first selected row

I have a UITableView where the user should be able to select (check) multiple rows.
I have an NSMutableArray in my controller to hold the selected items, and in my cellForRowAtIndexPath method, I check whether the item is in that array and return the cell in a checked/unchecked state accordingly.
Here's the code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = kContactCellReuseIdentifier;
static NSString *searchIdentifier = kContactSearchCellReuseIdentifier;
POContactCell *cell;
// Configure the cell...
if (tableView == self.tableView) {
cell = (POContactCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.contact = self.contacts[indexPath.row];
NSLog(#"Returned cell with name %#", cell.contact.name);
} else {
cell = (POContactCell*)[tableView dequeueReusableCellWithIdentifier:searchIdentifier forIndexPath:indexPath];
cell.contact = self.searchResults[indexPath.row];
}
if ([self.selectedContacts containsObject:cell.contact])
{
NSLog(#"was checked");
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
POContactCell* tappedCell = (POContactCell*)[self tableView:tableView cellForRowAtIndexPath:indexPath];
NSLog(#"Selected contact %#", tappedCell.contact.name);
if ([self.selectedContacts containsObject:tappedCell.contact]) {
// cell is already selected, so deselect it
NSLog(#"It's already selected, so deselect it");
[self.selectedContacts removeObject:tappedCell.contact];
tappedCell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
NSLog(#"It's not already selected, so select it");
[self.selectedContacts addObject:tappedCell.contact];
tappedCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:NO];
}
This code works... except for the first selection. The first cell that the user taps will get checked and will never get unchecked. I see from the log statements that all the cells are going through the exact same process and it's correctly recognizing the selection state of the first tapped row too, even though the accessory view doesn't reflect it.
After the first selection, all the other rows work perfectly.
Any debugging ideas?
You should be putting self.contacts[indexPath.row] (or self.searchResults[indexPath.row], as appropriate) in your array of selected items, and checking whether or not those objects exist or not in the array when the user taps a cell. You are almost doing that, it would appear, by setting cell.contact to the object from your data source and checking for cell.contact in your array. But I'd try putting the object directly into your array, e.g.
id contact = self.contacts[indexPath.row];
if ([self.selectedContacs containsObject:contact])
...
and stop checking if cell.contact is in the array to determine "selected-ness".
In a UITableView there is a small set of actual UITableViewCell objects in memory, and they get re-used. The root of your problem could very well be this, because you are checking to see if cell.contact is in your set of selected items; when a cell is reused, unless you wrote your own prepareForReuse, the previous value of your custom attributes may not (likely will not) be cleared.
Does that make sense?

ipad pop over with text field to enter

i have designed a pop over (tableview style) for ipad. Now like in asp.net i want a option at the end, so that if user clicks it he can enter value into it. Can it be done?If so how? I have played with tableviewcellstyles and got a text box as well but for all options. If anybody has done, can you give me some ideas. Thanks.
EDIT : I should have mentioned i am using Monodevelop to develop this app.
One approach, assuming you're using a Storyboard to create view and set properties:
In your UITableView object properties set "content" property to "Dynamic Prototypes".
Then add an additional Table View Cell to your table from the storyboard editor's UI object list (just like adding another label or button or whatever).
You should now have two TableViewCell objects in your table view. If not, add another.
Give both table view cell objects different identifiers (that is, set property "identifier" for each to different strings).
Set up the first cell to display your normal content.
Set up the second cell to display your unique "last row" layout.
In your code, in cellForRowAtIndexPath return the "main" cell for most rows, but the alternate cell for the last row:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// rows are 0-indexed
if (indexPath.row == myDataSource.count) {
NSString *CellIdentifier = #"AlternateCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// populate contents...
}
else {
NSString *CellIdentifier = #"NormalRow";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// populate contents...
}
}
For that to work, you'll have to tell the TableView that there's one more row than you have actual data for:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return myDataSource.count +1;
}

Resources