I have a UITableViewController. I need to display a table cell followed by a cell that has an embedded UITextField. Basically i need to display the tableView datasource alternatively. Please help me on this.. Thanks.
EDIT #1: Imagine NSMutableArray *tableData; is the datasource for the table which has the following elements:
#"Apple"
#"Mango"
#"Watermelon"
I want the UITableView to be displayed as:
1st cell: Apple
2nd cell: text field to enter quantity
3rd cell: Mango
4th cell: text field to enter quantity
5th cell: Watermelon
6th cell: text field to enter quantity
7th cell: save button
Use dataSource method like this:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return ([m_tableData count]*2)+1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == [m_tableData count]*2)
{
//cell with button
}
else
{
if(indexPath.row == 0 || indexPath.row%2 == 0)
{
//cell with label for displaying values
}
else
{
//Load a cell that has an embedded UITextField
}
}
}
Related
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;
}
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.
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
}
}
This question already has an answer here:
How to make a Sub Table view in a TableViewCell
(1 answer)
Closed 9 years ago.
In one of the cell of a tableView, i want to put another tableView(toyTable for example). Can anyone help me in this? I have created a tableView (main one) and another tableview in my project. Now in my main tableView, i have many cells and in one of the cell, i want that another table(toyTable) to be present in the cell.
Lets say you have two tables.
Make properties for both of them
// This one might be connected as an outlet with your storyboard
#property (nonatomic) IBOutlet UITableViewController *mainTable;
// This one should be in the .m file
#property (nonatomic) UITableViewController *toyTable;
Implement delegate and datasource protocol methods for each.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView == self.mainTable) {
return 2;
} else {
return 1;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.mainTable) {
return 10;
} else {
return 5;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.mainTable) {
// If this is the indexPath for the cell that should contain
// the nested table view, initialize your self.toyTable, set
// datasource and delegate etc.
// Else configure your default outer cell
} else {
// Configure the cells of your self.toyTable;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.mainTable) {
// If this is any normal outer cell
return UITableViewAutomaticDimension;
// If this is the outer cell that contains self.toyTable
return some bigger value (if desired)
} else {
// The height of the inner cell
return UITableViewAutomaticDimension;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.mainTable) {
// If this is the outer cell that contains self.toyTable
// You should ignore the selection
// Else handle it according to your needs
} else {
// Handle click on inner cell
}
}
If you are using a static table view as your mainTable, you can directly drag a table view onto one of your static cells and define prototype cells etc for it.
Putting table view inside table cell is a bad design. Insert toyTable as a section of main table view instead.
In cellForRowAtIndexPath add that table and do the code like this :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == mainTable) {
//do this for main table
}
else {
//do this for second table
}
}
Why are u doing this because its a bad idea for nesting the tableview instead UITableview already having expand and collapsing property.
Use below link for reference http://www.cocoanetics.com/2011/03/expandingcollapsing-tableview-sections/
i have custom tableviewcell which has a textfield inside for user input text, now how can i find cursor is in which cell?
tableview willBeginEditingRowAtIndexPath and tableview willSelectRowAtIndexPath does not return the correct index.
in editingStyleForRowAtIndexPath i want to retun UITableViewCellEditingStyleDelete just for cell that cursor is inside. how can i do that??
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(self.uiTableView.editing)
{
return UITableViewCellEditingStyleDelete;
}
else
return UITableViewCellEditingStyleNone;
}
So thanks inadvance
in the customecell, put id that carry the index , and in creation of table when create the cell set this id with the value
http://adeem.me/blog/2009/05/30/iphone-sdk-tutorial-part-6-creating-custom-uitableviewcell-using-interface-builder-uitableview/