i have a menu that contain 6 cells 1st one contain UIViewController the second contain UITableView
when i click the first cell a new page appear,and it contain a button
when i click this button i want access to the second cell from the Menu, that contain a tableView
// use this code for to Refresh or delete or insert TableviewCell
// reloading cell
NSIndexPath *tmpIndexpath=[NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:tmpIndexpath, nil] withRowAnimation:UITableViewRowAnimationTop];
// inserting cell
NSIndexPath *tmpIndexpath=[NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:tmpIndexpath, nil] withRowAnimation:UITableViewRowAnimationTop];
// deleting cell
NSIndexPath *tmpIndexpath=[NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:tmpIndexpath, nil] withRowAnimation:UITableViewRowAnimationBottom];
As much i understand your question you want to move from a tableview to other view on click..So,you have to use code similar to this..
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
Detail *img1=[[Detail alloc]initWithNibName:#"Detail" bundle:Nil];
img1.labelString = [titleArray objectAtIndex:indexPath.row];
[self presentViewController:img1 animated:YES completion:nil];
}
Write following coed in your UIButton Tapped Method.
-(void) ButtonTapped:(UIButton *) sender
{
NSIndexPath *indexpathNew = [self.tblView indexPathForCell:(UITableViewCell *)[sender superview]];
NSLog(#"%#",indexpathNew.row);
}
indexpathNew is return indextPath of selected button of cell.
Try my code i might be helpful to you.
Related
I have a UISearchController and i want to select the first row of the searchResultsController tableview on click of search button in the keyboard.
For that I tried implementing
-(void)searchButtonClicked:(UISerachBar*)searchBar {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.searchResultsController.tableView selectRowAtIndexPath: indexPath animated:false scrollPostion:UITableViewScrollPositionNone];
[self.searchResultsController tableview:self.searchResultsController.tableView didSelectRowAtIndexPath: indexPath];
}
but it didn't work for me!!
Then I tried to make a separate method:
-(void) plotPinOnMapWithIndexPath:(NSIndexPath*)indexpath;
and called it in didSelectRowAtIndexPath and in searchButtonClicked method by passing the indexPaths.But the problem is that the cell doesn't get highlighted the way it gets highlighted when user clicks on it.
This works for me, and is correct:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
optionally try this:
[self.tableView:self.tableView didSelectRowAtIndexPath:indexPath];
or you can use Segue to continue with result:
[self performSegueWithIdentifier:"showDetailView" sender:self];
In prepareForSegue method you pass parameters....
It worked!!
-(void)searchButtonClicked:(UISerachBar*)searchBar {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self tableview:self.searchResultsController.tableView didSelectRowAtIndexPath: indexPath];
}
I have uitableview and it has 1 button "TOP" in each row. I want to when user click on this button, this row has been push on top in Uitableview ( with animation). Can i reuse BVReorderTableView to do that? How can i do that? Thanks much
i am assuming your -cellForRowAtIndexPath loads cell contents from an array such as the one i named arrObjects which:
is an NSMutableArray object
has many NSString objects
something like:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//...
[cell.textLabel setText:[arrObjects objectAtIndex:indexPath.row]];
UIButton *btnUp = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnUp setFrame:CGRectMake(0, 0, 30, 30)];
//[btnUp setTag:100];
[btnUp setTitle:#"\u261D" forState:UIControlStateNormal];
[btnUp addTarget:self
action:#selector(moveToTop:)
forControlEvents:UIControlEventTouchUpInside];
[cell setAccessoryView:btnUp];
return cell;
}
- (void)moveToTop:(UIButton *)sender
{
UITableViewCell *cell = (UITableViewCell *)sender.superview; //iOS6 prior
//UITableViewCell *cell = (UITableViewCell *)sender.superview.superview; //iOS7
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
//uncomment lines for safety, incase indexPath ever comes nil... prevent the crash
//if (indexPath == nil) {
// return;
//}
//1. move and animate row to top
[self.tableView moveRowAtIndexPath:[NSIndexPath indexPathForItem:indexPath.row
inSection:indexPath.section]
toIndexPath:[NSIndexPath indexPathForItem:0
inSection:indexPath.section]];
//2. make appropriate changes to the datasource
//so that it reflects logically and is not just an aestehtical change
//PRE-NOTE:
//arrObjects is an object of a category on NSMutableArray
//with a custom instance method named -moveObjectFromIndexPath:toIndex:
//uncomment the following line when you are ready with the category
//[arrObjects moveObjectFromIndex:indexPath.row toIndex:0];
}
the category is easy to create, follow:
http://www.icab.de/blog/2009/11/15/moving-objects-within-an-nsmutablearray/
other links to try:
http://adeem.me/blog/2009/05/29/iphone-sdk-tutorial-add-delete-reorder-uitableview-row/
http://learn-iphone-app-development.com/2011/01/31/tables-part-iii-%E2%80%94-re-ordering-moving-cells-and-swipe-to-delete/
have you tried
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
you can use animations if you want so..
You can easy do that like this:
[self.tableView setContentOffset:CGPointMake(0, 0) animated:YES];
or:
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
or:
[self.tableView scrollRectToVisible:CGRectMake(0.f, 0.f, CGRectGetWidth(self.tableView.frame), CGRectGetHeight(self.tableView.frame)) animated:YES];
Edit
Sorry, I misunderstood what you meant.
You should do that like this:
[tableView moveRowAtIndexPath:indexPath toIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
If you want to tableView scroll to the top immediately after move the cell, you can try the above code.
Edit
// A more detailed description
- (void)buttonHandle:(UIButton *)sender {
// Assuming button is added directly on the cell.
UITableViewCell *cell = (UITableViewCell *)sender.superview;
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
[tableView moveRowAtIndexPath:indexPath toIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
}
The matter is, I want to have a grouped table view, and when I click on a cell, I want to have an another cell to appear under it, and so on. Everything is fine with workflow, but I have same problems with animations. It works very slowly and I can see all changes
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(#"selected section:%d selected row:%d",indexPath.section,indexPath.row);
NSIndexPath* pathToDelete;
[_tableView beginUpdates];
if (_selectedPath != nil && [_selectedPath isEqual:indexPath]){
//If the same row was selected
pathToDelete = [NSIndexPath indexPathForRow:_selectedPath.row+1 inSection:_selectedPath.section];
_selectedPath = nil;
[_tableView beginUpdates];
[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:pathToDelete] withRowAnimation:UITableViewRowAnimationTop];
[_tableView endUpdates];
} else {
//If not
if (_selectedPath != indexPath){
//delete the row that we selected last time
if (_selectedPath != nil){
pathToDelete = [NSIndexPath indexPathForRow:_selectedPath.row inSection:_selectedPath.section];
[_tableView beginUpdates];
[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:pathToDelete] withRowAnimation:UITableViewRowAnimationTop];
[_tableView endUpdates];
}
//add new row in the section we selected
_selectedPath = indexPath;
[_tableView beginUpdates];
[_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
[_tableView endUpdates];
}
}
[_tableView beginUpdates];
[_tableView deselectRowAtIndexPath:indexPath animated:NO];
[_tableView endUpdates];
[_tableView reloadSections:[[NSIndexSet alloc] initWithIndex:pathToDelete.section] withRowAnimation:UITableViewRowAnimationMiddle];
[_tableView reloadSections:[[NSIndexSet alloc] initWithIndex:_selectedPath.section] withRowAnimation:UITableViewRowAnimationMiddle];
[_tableView endUpdates];
}
I think the problem is with begin & endUpdates method, but I don't know where exactly. Any clue?
Thanks
You could do this much simpler, by using a single beginUpdates - endUpdates. You just make your deletions and insertions inside of it. Don`t forget to update the data source for the table, otherwise the app will crash.
Hope this helps!
I want to add cells into UItableView like in mail.app on ipad. In my cells I have UITextFields, and I have change this method to set focus
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath{
NSLog(#"%d",newIndexPath.row);
UITextField* field = (UITextField *) [tblView viewWithTag: newIndexPath.row];
int l_cellNum = field.tag;
if(l_cellNum == 1 && isAddedNewRow == FALSE){
isAddedNewRow = TRUE;
[cellsArray insertObject:#"CC" atIndex:1];
[cellsArray insertObject:#"BCC" atIndex:2];
CGRect tvbounds = [tblView bounds];
[tblView setBounds:CGRectMake(tvbounds.origin.x,
tvbounds.origin.y,
tvbounds.size.width,
tvbounds.size.height)];
NSIndexPath *path1 = [NSIndexPath indexPathForRow:1 inSection:0];
NSIndexPath *path2 = [NSIndexPath indexPathForRow:2 inSection:0];
NSArray *indexArray = [NSArray arrayWithObjects:path1,path2,nil];
[tblView insertRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationTop];
[tblView reloadRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
}
[field becomeFirstResponder];
}
But when I click to one of the cell i add 2 cells (like mail.app) but my tags for textFields do not updates, and I cant to set focus on it =(. If I call reloaddata method it calls -cellForRowAtIndexPath with this new cells at the beggining and after for all cells, and it will not work too.
It sounds like what you're looking for is a built-in style of cells called UITableViewStyleSubtitle, where you can customize the subtitle just like the iPad Mail.app. Take a look at this page in the iOS documentation. Then, you can set the subtitle of the UITableViewCell to the text that you need.
Additionally, the iOS doc has more information about customizing table view cells.
I'm trying to create a simple chat application for iOS. It currently looks like this:
I want to change the order that the messages is displayed, i.e. display the latest message over the older messages. My current implementation looks like this:
// Datasource for the tableView
messages = [[NSMutableArray alloc] init];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[...]
// messageTextView is a contentView subView.
messageTextView.text = [messages objectAtIndex:indexPath.row];
[...]
- (IBAction)sendMessage:(id)sender
{
[messages addObject:messageField.text];
messageField.text = #"";
[self.tableView reloadData];
/*
I have tried the implementation below, but I always got an exception.
[self.tableView beginUpdates];
NSIndexPath *path1 = [NSIndexPath indexPathForRow:1 inSection:0];
NSArray * indexArray = [NSArray arrayWithObjects:path1,nil];
[self.tableView insertRowsAtIndexPaths:indexArray
withRowAnimation:UITableViewRowAnimationTop];
[self.tableView endUpdates];
*/
}
Any tips on how to do this would be great.
Thanks.
Simple you need to insert the new UITableViewCell at the 0 index. Also modify your Datasource otherwise your app will crash. Below I show how to modify your UITableView. Modifying your datasource is easy.
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[tableView beginUpdates];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
[tableView endUpdates];
What you are essentially doing here is inserting the new chat message cell at the 0th index position. You could use a nice animation effect to make it appear or fade in.
There are various animations that you can use here-
UITableViewRowAnimationBottom
UITableViewRowAnimationFade
UITableViewRowAnimationMiddle
UITableViewRowAnimationNone
UITableViewRowAnimationRight
UITableViewRowAnimationTop
You can try.
[messages insertObject:messageField.text atIndex:0];
instead of [messages addObject:messageField.text];.
Swift Solution
Define array like this
var arrMessage = [AnyObject]()
and Button Action
#IBAction func btnSendMessageTapped(sender: AnyObject) {
arrMessage.insert(txtTypeMessage.text!, atIndex: 0)
txtTypeMessage.text = ""
self.tblMessage.reloadData()
}