TableView adding a cell - iphone

I'm doing a project in which I have a TableView in which I have different Rows/Cell and in each cell there is a add button. What I need to do is on clicking the add button the new row should add below the row in which I clicked the button, means I have to add a cell to the next of the selected button on the row.and also a user can enter a text on the new row. Please tell me how to do this . As I'm new in iPhone development. I will be very thankful ....
Here is a click button function in which I want to perform an action to add new cell.
- (IBAction)AddButtonAction:(id)sender
{
[_choices insertObject:#"avav" atIndex:[_choices count]];
[self.tableView reloadData];
NSLog(#"here");
}

Use this delegate method to add a new row below the selected row. And use custom cell to have a text field on it...
rowCount ++;//Here rowcount refers the no. of rows in the table.
selectedIndexPath = indexPath;//Assign the selected indexpath for creating custom cell on it.
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];//Indexpath refers the currently selected/targeted cell.
In cellforRowAtIndexPath use like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if((selectedIndexPath) && (indexPath.row == selectedIndexPath.row) && (indexPath.section == selectedIndexPath.section))
{
static NSString *cellIdentifier=#"cell";
NewCell *cell = (NewCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSString *customeCellName = [NSString stringWithFormat:#"%#",[NewCell class]];
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:customeCellName owner:self options:nil];
for (id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[UITableViewCell class]])
{
cell = (NewCell *) currentObject;
break;
}
}
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
else
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:#"%d",indexPath.row];
// Configure the cell...
return cell;
}
}
OutPut will be like this
need to customize as per your requirement.

I consider you have textfeild in all cells. In AddButtonAction function save index of row. While creating cells make selected cell textfeild firstresponder. Once user enter data save it in array and again reload. Just keep track of selectedIndex properly.
Ifeel this will help.

If you add button to cell by addSubview method, simply you can get indexPath with the following code:
- (IBAction)AddButtonAction:(id)sender
{
UIButton *button = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell *)[button superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
// Adding methods comes here...
}
If you are using some addSubview levels, you should use same superview methods inverse to access the UITableViewCell level. If using custom cells, replace your custom cell classname with UITableViewCell in the above code.
For adding cells, take a look at WWDC 2011 Videos, "UITableView Changes, Tips & Tricks" video. It shows some nice and useful methods to insert, delete and reload new rows/sections between other rows/sections like insertRowsAtIndexPaths:withRowAnimation: method. Or see Apple Documents.

Related

How to Add action event work when click on customize list cell button in iphone

Create a Table View.
then create a customize table View cell and place a button on cell and Now I try to get Table View index when click on index.
But when I click on button that place on cell It's not giving me the Index of list.
My Table View class name is SubMenuViewController and cell class name is SubMenuCell
and my code of SubMenuViewController is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ModelLocator *model = [ModelLocator getInstance];
static NSString *simpleTableIdentifier = #"SubMenuCell";
SubMenuCell *cell = (SubMenuCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SubMenuCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if (model.foodSubItemList) {
FoodSubItemVO* foodSubItemTemp = [model.foodSubItemList objectAtIndex:indexPath.row];
[cell.lbSubFoodItem setText: foodSubItemTemp.foodSubItemName];
[cell.lbPrice setText: foodSubItemTemp.price];
[cell setIndexPath:indexPath];
}
return cell;
}
and My SubMenuCell code here
- (IBAction)addItemIntoOrder:(id)sender {
NSLog(#"#%",indexPath);
NSLog(#"#%",indexPath.row);
}
indexPath declare in SubMenuCell.h
Your NSLog's are faulty; try this instead (mind that changed % and # order).
- (IBAction)addItemIntoOrder:(id)sender
{
NSLog(#"%#",indexPath);
NSLog(#"%#",indexPath.row);
}
Make sure that the addItemIntoOrder: action is hooked up to your delegate correctly in IB.

IOS UItableview reloadRowsAtIndexPaths refresh issue

I want to update to tableview custom cell. I want to refresh table cell When I search. I use reloadRowsAtIndexPaths methods. This method work, But did not update cell. Can you help me please
Below method run When I searched
-(void)doSearchHotelName:(id)sender{
NSIndexPath *tmpIndexpath=[NSIndexPath indexPathForRow:1 inSection:0];
[self.tableV beginUpdates];
[self.tableV reloadRowsAtIndexPaths:[NSArray arrayWithObjects:tmpIndexpath, nil] withRowAnimation:UITableViewRowAnimationFade];
[self.tableV endUpdates];
}
Below method table method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"Hotel_FilterSearchCell";
Hotel_FilterSearchCell_iPad *cell = (Hotel_FilterSearchCell_iPad *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib ;
nib = [[NSBundle mainBundle] loadNibNamed:#"Hotel_FilterSearchCell_iPad" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
else if ([indexPath row]==1) {
if([SharedAppDelegate.filter_selected_hotels_list count]==[SharedAppDelegate.filter_hotels_list count])
[cell.cellExtraInfo setText:#"All(H)"];
else if ([SharedAppDelegate.filter_selected_hotels_list count]!=[SharedAppDelegate.filter_hotels_list count])
[cell.cellExtraInfo setText:[NSString stringWithFormat:#"%i %#",[SharedAppDelegate.filter_selected_hotels_list count],#"Selected(H)"]];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
Try changing UITableViewRowAnimationFade to UITableViewRowAnimationNone and let me know if that helps.
I just had to do the same because I'm using a static UITableView in my storyboard and it seems any animation moves the old cell out of view and replaces with the new cell. This basically removes the cell from the table because the old cell and the new cell are the same object(my assumption).
From UITableView documentation:
beginUpdates
Begin a series of method calls that insert, delete, or select rows and sections of the receiver.
You should not use this method unless you are inserting, deleting or selecting a row or a section.

dequeueReusableCellWithIdentifier not returning cell of my custom type

I am using ios5 storyboards with a UITableViewController with a UITableViewCell subclass. I do not want to design the visual elements of the cell in the storyboard designer for the view, because I want to use a reusable subclass of UITableViewCell (specifically TDBadgedCell).
I have set my cell identifier in the storyboard designer, and all of the rows load correctly in the UITableView as long as I'm not setting any of the properties unique to TDBadgedCell. If I set the badgeString property though which is unique to TDBadgedCell, I get an exception. I narrowed down that dequeueReusableCellWithIdentifier: is not returning a cell of type TDBadgedCell.
I'm only running into this with a UITableViewController. I have a UIViewController with an embedded UITableView set up in the same fashion and it's not an issue. Any ideas?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString *CellIdentifier = #"PhoneNumberCell";
TDBadgedCell *cell = (TDBadgedCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[TDBadgedCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if ([cell isKindOfClass:[TDBadgedCell class]])
{
NSLog(#"It is TDBadgedCell");
}
else
NSLog(#"It is NOT TDBadgedCell");
I had a similar issue in that I am subclassing UITableViewCell but not using storyboard. Here is my solution to using different cell classes dependent on if the user had purchase the unlock feature of the app. Hope it helps someone.
In a nutshell, I had the cell with several objects including a UITextView object. I wanted to lock down the copy and paste feature of the UITextView object in the lite version but then release the feature once the user had purchased in in-app product.
I had two UITableViewCell classes, one with UITextView as it is and another with UITextView subclassed with canBecomeFirstresponder returning NO. That way the user could still scroll up and down the UITextview data but not copy and paste the data.
Here is the code and all I had to do was rename the reuse identifiers.
WHY? Because [self.tableview reloadData] would not rebuild the cells with the new class as the cell was still in existence. New cells off the screen would get the new class but existing ones would not. This solution rebuilds all cells once off after the purchase unlocking the added feature.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (your test if in-app was purchased is yes)
{
static NSString *MyIdentifier = #"MyCell";
FrontCell *cell = (FrontCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[FrontCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.shouldIndentWhileEditing = NO;
}
//....///
cell.trackDetails.text = [yourObject objectAtIndex:indexPath.row];
cell.trackDetails.delegate = self;
cell.trackDetails.tag = indexPath.row;
return cell;
}
else // inapp not purchased
{
static NSString *MyLockedIdentifier = #"MyLockedCell";
FrontCellLocked *cell = (FrontCellLocked *)[tableView dequeueReusableCellWithIdentifier:MyLockedIdentifier];
if (cell == nil)
{
cell = [[FrontCellLocked alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyLockedIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.shouldIndentWhileEditing = NO;
}
//....///
cell.trackDetails.text = [yourObject objectAtIndex:indexPath.row];
cell.trackDetails.delegate = self;
cell.trackDetails.tag = indexPath.row;
return cell; }
}
In storyboard,you can set the Custom Class property for a subclass of UITablviewCell.
Then dequeueReusableCellWithIdentifier method will return cell with the type of your subclass.
I think you are using wrong method for dequeuing the cells.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [self.tblProfileInfo dequeueReusableCellWithIdentifier:#"PostCell" forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
You forgot for indexPath at the end.

Using a dynamic custom UITableViewCell in XCode 4.2, with Storyboards and UISeachDisplayController

I've used Xcode 4.2 to create a storyboard-based iOS application.
One of my screens contains a UITableViewController, using dynamic custom cells.
So far - so good.
Now, I wanted to add a UISearchDisplayController to allow filtering my list.
For some reason, the UISearchDisplayController won't display my custom cells, and I can't find a way to force it...
This is what my cellForRowAtIndexPath method looks:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"QueueListCell";
QueueListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[QueueListTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier];
}
assert(cell);
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]) {
indexPath = [_indexPathsForSearchResults objectAtIndex:indexPath.row];
}
// Set up the cell...
NSDictionary* itemDict = [_ListItems objectAtIndex:indexPath.row];
cell.labelQueueName.text = [itemDict objectForKey:kQueueName];
cell.labelQueueNumItems.text = [[itemDict objectForKey:kQueueNumItems] stringValue];
return cell;
}
Any thoughts on how to get this working? I mean, my UISearchDisplayController table DOES show the correct number of results (I know that since I can click on them, and I added an NSLog to let me know what I'm clicking on...)
This is my table view
This is how the search display table looks like...
My problem/question is how to make the UISearchDisplayController table view show my custom cells?
Any help appreciated...
Reuven
Answer specific to query
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}
For Complete example, have the sample code from apple's site.
Step by step illustration

Can I use UITableviewCell like this?

Normally in a UITableView, we assign the value for the cells in the cellForRowAtIndexPath method which is the delegate method of UITableView. In this case, if we use a CustomTableViewCell, we assign the values in cellForRowAtIndexPath as
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"CustomTableViewCell" owner:self options:nil];
cell = customTableViewCell;
}
cell.label1.text = #"text1";
cell.label2.text = #"text2";
// Configure the cell...
return cell;
}
This way of using CustomCell is a usual one. The texts will be displayed on the labels of the cell, but I need some clarification in using CustomCell in another way. I have a method in MainViewController like the following
- (void)methodInMainViewConroller {
CustomCell *customCell = [[CustomCell alloc] init];
[customCell methodInCustomCell];
[tableView reloadData];
}
and in CustomCell, I have the definition for the method as
- (void)methodInCustomCell {
self.label1.text = #"text1";
self.label2.text = #"text2";
}
Will this work? Will the text be displayed on the labels of the cell? Actually it doesn't work for me. Anyone help me to make this work perfectly. Thanks in advance.
You can try this
- (void)methodInMainViewConroller {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
CustomCell *customCell = (CustomCell *)[self.tableView cellForRowAtIndexPath:indexPath];
[customCell methodInCustomCell];
}
UPDATE: What I've just realized is that once you scroll the table view your labels will be reset to previous values. It might not work this way, but give it a try.
Take a good look at your code. You never actually do anything with that cell once you set the labels. Anyway, how would the tableview know to use your cell and where to put it?
It doesn't work because the cell you alloc/init and work on is a totally different object. You can, however, store that cell as an ivar and always return that very cell for the given indexPath.
This method don't work because you call it for just created custom cell not for cell in tableView.
You need something like this:
CustomCell *customCell = [tableView cellForRowAtIndexPath:indexPath];
[customCell methodInCustomCell];