Is there a way to change the order of tableviewcells with animation?
I want to slide up or down cells and change the ordering.
But i dont know if it is possible with animation?
I need help.
Thanks,Tintin
You could do that UITableView's edit mode.
Iphone sdk tutorial Add/Delete/Reorder UITableView row.
In this code dataArray is the name of my array you change it with your own array name
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
NSString *stringToMove = [[self.dataArray objectAtIndex:sourceIndexPath.row] retain];
[self.dataArray removeObjectAtIndex:sourceIndexPath.row];
[self.dataArray insertObject:stringToMove atIndex:destinationIndexPath.row];
[stringToMove release];
[tableView reloadData];
}
Related
I have created a custom cell that places a UITextField in each row of a UITableView. What I would like to do is enable the UITextField for a particular row (i.e. activate the cursor in that textfield), when the user presses on anywhere inside the cell.
I have the following method where I try to do this:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[_cell.textField setUserInteractionEnabled:TRUE];
}
However, this is not working. Can anyone see what I'm doing wrong?
You're on the right track placing this inside didSelectRowAtIndexPath:. However, setting user interaction enabled on the textfield just means that the user would be allowed to interact with the object if they tried. You're looking for becomeFirstResponder.
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
[cell.textField becomeFirstResponder];
}
Try this code:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[_cell.textField becomeFirstResponder];
}
What is this _cell iVar? Are you setting it somewhere?
I think what you are trying to accomplish is this:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell.textField becomeFirstResponder];
}
Notice that in this way you are getting the cell for the tapped indexPath.
And also, just for information, it's a pattern in objective-C to use booleans as YES or NO, instead of true or false.
I have an application in which I am keeping my tableview as editable to drag and drop cells. But when I am doing that the design of the cell is changing such that every element is become centered and there is a right disclosure button of three lines. I need to do this by keeping the same design of the cell.without having any disclosure button and keeping the cell layout same as it is in the case of noneditable table view.I am implemented the editing like this [tableview setEditing:YES animated:YES]; in the didload and implemented the delegates as
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleNone;
}
- (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
NSInteger sourceRow = sourceIndexPath.row;
NSInteger destRow = destinationIndexPath.row;
id object = [array objectAtIndex:sourceRow];
[array removeObjectAtIndex:sourceRow];
[array insertObject:object atIndex:destRow];
}
Can anybody help me in acheiving this?
Give this a try:
Add this method to your tableView delegate to prevent indentation:
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath{
return NO;
}
Then in your UITableViewCell subclass set all reordering views' alpha to 0:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
[super setEditing:editing animated:animated];
if (editing) {
for (UIView * view in self.subviews) {
NSLog(#"%#",view);
if ([NSStringFromClass([view class]) rangeOfString: #"Reorder"].location != NSNotFound) {
for (UIView *sv in view.subviews) {
sv.alpha = 0.0f;
}
}
}
}
}
Please note that this is a hack and I'm not sure if apple would reject the app over this.
You should return NO to this method of the tableViewDatasource:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
I want to remove downloaded image in invisible tableview cell when I scrolled tableview.
So I set a tag
- (void)appImageDidLoad:(NSIndexPath *)indexPath
{
int row=(int)indexPath
UITableViewCell *cell;
cell=[self.gamesTableView cellForRowAtIndexPath:indexPath];
OneItemCell *cell1=(OneItemCell*) cell; //custom cell
cell1.imageview.tag= row+100;
}
I set remove downloaded image when selected row is last row cell(=add cell).
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(row==nArrayCount)
{
if(row>19)
{
for(int i=1;i<row-8;i++)
{
[[self.gamesTableView viewWithTag:i+100] removeFromSuperview];
}
}
}
but it is not working . because [[self.gamesTableView viewWithTag:i+100] is NULL.
another way I set a key to cell's imageView by NSMutableDictionary(=imageRemoveInProgress)
- (void)appImageDidLoad:(NSIndexPath *)indexPath
{ .
.
[imageRemoveInProgress setObject:cell1.imageView forKey:[NSNumber numberWithInt:row+100]];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ .
.
[[imageRemoveInProgress objectForKey:[NSNumber numberWithInt:row+100]] removeFromSuperview];
}
it is not working too . because[[imageRemoveInProgress objectForKey:[NSNumber numberWithInt:row+100]] is NULL too.
[[imageRemoveInProgress objectForKey:[NSNumber numberWithInt:row+100]] has value in appImageDidLoad.
but In - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
it has NULL.
why do taged and seted key value cell's imageView become NULL?
how to remove tableViewcell's imageview?
I don't know the exact way, But I can provide a hint to do it
you can use something like this
NSArray *visiblePaths = [self.tableView indexPathsForVisibleRows];
visiblePaths is an array of indexPaths of cells that are currently visible, you can check whether the cell for which you want to remove the image lies in this array or not.
I followed Simon Lee's tutorial on animating uitableview height change, and it's working amazing! The only problem I seem to be facing is that I can tap several cells, and they all will remain expanded regardless if another is already expanded. I only want one expanded at any given time, and if another is already expanded, for it to be closed before a new one expands.
Simon mentioned that we should "store a single NSIndexPath as the current selected item instead on an array of indexes." I'm not exactly sure how to do this. If anyone could help out that would be awesome!
what you have to do is just add the code below in the tableView delegate method tableView:didSelectRowAtIndexPath: below the line
[selectedIndexes setObject:selectedIndex forKey:indexPath];
code to add
NSArray *key = selectedIndexes.allKeys;
for (int i = 0; i < [key count]; i++) {
if (![[key objectAtIndex:i] isEqual:indexPath]) {
[selectedIndexes setObject:[NSNumber numberWithBool:NO] forKey:[key objectAtIndex:i]];
}
}
Happy Coding :)
Enjoy the Day!!
Yes you can colapse the cells using the delegate methods of UITableView -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath and - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath :-
First declare the variable to get theindexPath
NSIndexPath *selectedCellIndexPath;
then get the cell's indexPath in didSelectRowAtIndexPath :-
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedCellIndexPath=indexPath;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];
}
Then do the collapsing and extending and set the lower and upper height:-
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(selectedCellIndexPath != nil
&& [selectedCellIndexPath compare:indexPath] == NSOrderedSame)
return yourCellsExtendedHeightInInteger;
return yourCellsCollapsedHeightInInteger;
}
or use
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(selectedCellIndexPath && indexPath.row == selectedCellIndexPath .row)
{
return 100;
}
return 44;
}
You can see this tutorial also:-
I am making an app in which I want to move the row.So I can do it by
[self.tableList setEditing:YES animated:YES];
where "tableList" is my table's name but whenever I am running this line.I am not able to get the icon for move which usually comes at right side for moving a cell instead I am getting the icon to delete a row which comes on left side of a cell.
Please help me to get my cell move and ask if you need any clarification of my question..
Thanks.
You need to implement the below two methods
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
//do code according to your requirements of app
//if you do not write ant thing in the method then also your row will be moved.
NSString *stringToMove = [[self.dataArray objectAtIndex:sourceIndexPath.row] retain];
[self.dataArray removeObjectAtIndex:sourceIndexPath.row];
[self.dataArray insertObject:stringToMove atIndex:destinationIndexPath.row];
[stringToMove release];
[tableView reloadData];
}
use this link of aple and scrol down to apple how to move or scrol rows. cheerz :)
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html
– beginUpdates
– endUpdates
– insertRowsAtIndexPaths:withRowAnimation:
– deleteRowsAtIndexPaths:withRowAnimation:
– moveRowAtIndexPath:toIndexPath:
– insertSections:withRowAnimation:
– deleteSections:withRowAnimation:
– moveSection:toSection:
– scrollToRowAtIndexPath:atScrollPosition:animated:
– scrollToNearestSelectedRowAtScrollPosition:animated:
use these two method for row move
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath*)indexPath
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath