How to implement a scroller in UITableView with only ONE section? - objective-c

I have lots of data in a UITableView but I don't want any of it in sections. However, I would like to implement a scroller in my table view that works in the same manner as
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
//...
}
does, but without having sections. I want my scroller to scroll to specific numbers associated with the data in each cell of the table view.
Has anyone done this and can point me in good direction?
Thanks in advance!

Assuming your scroller can get the index of the selected data, you can use [UITableView scrollToRowAtIndexPath:atScrollPosition:animated:]. The code might look something like this:
-(void)jumpToSelectedIndex:(NSInteger) selectedIndex {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:selectedIndex inSection:0];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:YES];
}

Related

tvOS UITableView scroll to cell

I'm starting a new project for tvOS. I have created a programmatically UITableView inside my viewcontroller. Inside the UITableView i have created 5 cells. Until now everything works, but the problem happens when i'm trying to programmatically scroll to cell 4 so i'm doing:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:3
inSection:0];
[tableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
But the code above does nothing. It always start at first cell and highlight it as the selected one. I want to be able to programmatically start at another cell and it most be highlight so when i use the arrow keys down or up it scroll from there and not first cell. Please help me in doing that
You should do next:
In viewController that respond for UITableView, implement next function:
- (UIView *)preferredFocusedView {
return self.tableView;
}
where tableView is your table, and after that, override next function
- (NSIndexPath *)indexPathForPreferredFocusedViewInTableView:(UITableView *)tableView {
return [NSIndexPath indexPathForRow:_exerciseIndex inSection:0];
}
where _exerciseIndex index that you need to focus.

Indexpath of the button from UICollectionView

I tried to:
- (IBAction)delete:(UIButton*)sender{
NSIndexPath *indexPath = [self.collectionView indexPathForCell:(TourGridCell *)[[[sender superview]superview]superview]];
}
But NSLog shows that cell exist, but indexpath is nil.
OK, here it is:
- (IBAction)delete:(UIButton *)sender{
NSIndexPath *indexPath = nil;
indexPath = [self.collectionView indexPathForItemAtPoint:[self.collectionView convertPoint:sender.center fromView:sender.superview]];
}
Sometimes it's the simplest possible answer. I was having exactly the same problem as #Schmidt, but was frustrated to see that his answer was to use indexPathForItemAtPoint:, as though indexPathForCell: was somehow broken and couldn't be used as intended.
Then I tried his solution, and still had the same result: the index path was coming back nil.
Solution: the view controller's collectionView outlet wasn't connected to the UICollectionView instance in the NIB (on the storyboard). After making that missing connection, both methods (indexPathForCell: and indexPathForItemAtPoint) worked as expected.
I know other devs run into this problem sometimes, so take this as a reminder: the problem may not be your code, per se, but rather something in Interface Builder like an unconnected outlet (or just as confusing, an outlet that's connected to something which no longer exists).
Swift version of the answer
var indexPath: IndexPath? = nil
indexPath = collectionView.indexPathForItem(at: collectionView.convert(sender.center, from: sender.superview))
Another best way is to subclass the UIButton and add an NSIndexPath property to it.
When loading the cell in
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath method
add this statement.
yourCell.customButton.indexPath = indexPath;
And
- (IBAction)delete:(UIButton *)sender
{
NSIndexPath *indexPath = sender.indexPath;
}

UITable view selection stay highlighted

I am using XCode4.2 to develop a storyboard application
I am switching views based on a click in a UITableView after clicking the row is highlighted in blue and the new view is loaded .
when I come back to the original view the row is still highlighted in blue ... how can I disable that ?
I do this in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath];
}
That way I have the index path that was selected every time.
use deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated function to deselect rows in uitableview
call it in your viewWillAppear function
[yourTableView deselectRowAtIndexPath:selectedIndexPath animated:NO];
if you are not sure which is index you select last time you can run a loop on all the index path in your table.
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
for(int i = 0; i <= [yourArr count]; i++)
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
[yourTableView deselectRowAtIndexPath:indexPath animated:NO];
}
}
Note: As I don't know your exact code....you need to modify above code to your needs.
A UITableViewController does this automatically. Otherwise, in your viewDidAppear for the owning controller, you need to call - (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated on the table view.

tableview won't update after adding objects to an array

I have a tableview inside a popover in my main view which is a web browser. Whenever the user visits a web page I want it to add the page to a list of recently visited websites. I have the code set up to add the URL as a string to the array that is the data source for the table view. The table view only shows the first site visited and won't show anything past that. However I know the sites are being added to the array because the array shows the Sites using NSLog statements after being added. However they still won't show up in the table. Can anyone help me?
EDIT:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [recentlyVisitedUrls count];
}
Also make sure you are returning the correct length of your array from – tableView:numberOfRowsInSection:
Try
[tableView reloadTable];
after you add the new data.
If you wan't animated updating, you can call:
[self.tableView performSelectorOnMainThread:#selector(reloadData) withObject:nil waitUntilDone:NO]
If you need animation, it will be better read: Table View Programming Guide for iOS, you need Batch insert… part.
If you have a large table, [tableView reloadData] is probably not ideal. It'll also will reset your view to the beginning of the table, which may or may not be desirable. You might try something like this:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_model.recordCount - 1 inSection:0];
[_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
[_tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];
BTW, _model is the data delegate class for the table in the example.
Also, make sure the data delegate updates it's total count correctly before you insert, or your app will crash on the insertRowsAtIndexPaths.

UITableView Center Cell Details

I am trying to sync a UITableView with a UILabel to make sure they show the same data; of course, things will be different in the end, but for testing this is what I need to do.
See the arrow? I want that middle cell (from px44-88) to show the cell.textLabel.text in a UILabel when it is the "middle cell".
I tried using - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath but I was having so many problems I figured I'd come here to ask if anyone has a better way of doing this. I'm not sure if it would make a difference or not but I am using NSFetchedResultsController to populate my UITableView.
UITableView is a subclass of UIScrollView, so probably you can use UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
member to detect which cell is currently in the middle.
I.e. assuming you have a plain table with no sections, and tableView is an outlet for the table, label is an outlet for the label, than this will function in your controller will work:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
int row = (scrollView.contentOffset.y + tableView.frame.size.height / 2) / 44;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
label.text = cell.textLabel.text;
}
Of course, you need to do some scrolling to make it work ;)
You can use visibleCells method to get layout of visible cells for non-plain table and use it to detect cell in the middle of the table.