IOS UItableview reloadRowsAtIndexPaths refresh issue - iphone

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.

Related

TableView adding a cell

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.

table view when i deleting 1st row one more row is adding at the last cell which is last cell information

I have created a table view. Each of my row has a delete button. Now, when I am deleting first row, the last row is being added one more time to the table view.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
if(currentHTMLElement==#"1") {
static NSString *CellIdentifier=#"Cell";
UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}
NSMutableDictionary *d = (NSMutableDictionary *) [arr objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
retun cell;
}
}
I have some more labels in each row. Also, I reload the table view.
May be replace currentHTMLElement==#"1" with [currentHTMLElement isEqualToString:#"1"]
Make sure you delete it in this way:
[self.tableView beginUpdates];
//delete item form you array (arr)
//delete row from tableView
[self.tableView reloadData];
[self.tableView endUpdates];

dequeueReusableCellWithIdentifier not working with ARC and IOS 4

I can't seem to get dequeueReusableCellWithIdentifier working.
I need to build a project for IOS 4 so I can't use storyboards, but I'm using ARC.
Let's say I have 2 sections, each with 1 row.
Looking at the code below, I'm using the strong property to pass ownership, since ARC would insert the "autorelease" code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"TableCellIdentifier";
MainTableCell *cell = (MainTableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
self.retainedCell = [[MainTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
However, for each row, cell is always nil (and hence a new MainTableCell is alloc'd) each time the function is called. The cell is never re-used.
This wouldn't be so much a problem except I programmatically call tableView:cellForRowAtIndexPath:, meaning I get a newly alloc'd cell each time, rather than the existing cell.
The only method I can see is to add the cells to an NSMutableArray.
Is there something I'm missing with dequeueReusableCellWithIdentifier now?
Thanks!
EDIT
I'm using the code below to get the cell. As mentioned it's creating a new cell not re-using the one that should have been already made + retained.
I don't need to invoke reloadData for all the rows, just change a specific one.
MainTableCell *cell = (MainTableCell *)[self tableView:self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
[self configureCell:cell atIndexPath:indexPath];
You happen to be de-queuing MainTableCell, and then you proceed to check if it is nil, at which point you use a completely different var to alloc a table cell. What the heck? Try this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"TableCellIdentifier";
MainTableCell *cell = (MainTableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[MainTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}

dequeueReusableCellWithIdentifier: always returns 'nil' for visible cells

I create a custom table view cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITextField *editField=nil;
...
NSString *CellIdentifier = [NSString stringWithFormat:#"cell:%d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
// Configure the cell...
switch (indexPath.row) {
case 0:
{
cell.textLabel.text=DEVNAME_TEXT_NDVC;
cell.textLabel.font=[UIFont boldSystemFontOfSize:LABEL_TEXTSIZE_NDVC];
editField=[[UITextField alloc] initWithFrame:CGRectMake(158, 9, cell.frame.size.width-183, cell.frame.size.height-15) ];
editField.tag=DEVNAME_TAG_NDVC;
...
[cell.contentView addSubview:editField ];
[editField release];
}
break;
The table has 5 lines only, and each of them is on the screen always.
Later, when I try to get access to the cell I always get 'nil'
The following code should place cursor to apropriate UITextField when user tap the cell, but it doesn't, since 'cell' is always =0.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat:#"cell:%d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UITextField *tf=nil;
[tableView deselectRowAtIndexPath: indexPath animated: YES];
[activeField resignFirstResponder]; // Last used UITextField
switch (indexPath.row) {
case 0: //
tf=(UITextField*)[cell.contentView viewWithTag:DEVNAME_TAG_NDVC];
[tf becomeFirstResponder]; // Show the keyboard
//[tf performSelector:#selector(becomeFirstResponder) withObject:nil afterDelay:0.7];
break;
Please, could you suggest what is wrong? Why [tableView dequeueReusableCellWithIdentifier:CellIdentifier] always =0,
but all of the table cells are always visible.
Thanks.
Maybe I don't understand the question, but don't table cells only become reusable once they are no longer being displayed? If they are still visible, how could you reuse them?
Change this:
NSString *CellIdentifier = [NSString stringWithFormat:#"cell:%d",indexPath.row];
to:
static NSString *CellIdentifier = #“XXXX”;
Yes, dequeueReusableCellWithIdentifier: always return nil EXCEPT using registerNib:forCellReuseIdentifier:.

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];