UITableView sectionForSectionIndexTitle Method doesn't scroll tableview in iOS - ios

I am trying to index my UITableView with following code
In my ViewDidLoad
arrayOfTableView = [[NSArray alloc] initWithObjects:#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"10",#"11",#"12",#"13",#"14",#"15",#"16",#"17",#"18",#"19",#"20",#"21",#"22",#"23",#"24",#"25",#"26", nil];
And in my sectionIndexTitlesForTableView Method
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSArray *tempArray = [[NSArray alloc] initWithObjects:#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"10",#"11",#"12",#"13",#"14",#"15",#"16",#"17",#"18",#"19",#"20",#"21",#"22",#"23",#"24",#"25",#"26", nil];
return tempArray;
}
And in my sectionForSectionIndexTitle Method
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return index;
}
It's appear IndexBar beside of my tableview . However when i tap on IndexBar , it's doesn't scorll to there.
I have already test with NSLog, that event or not? It's event.
But don't scroll to tableView.
please help me to find out.

I think you need to check your numberOfSectionsInTableView: method.
Also you should implement this method (tableView:sectionForSectionIndexTitle:atIndex:) only for table views with a section index list—which can only be table views created in the plain style (UITableViewStylePlain).

Related

Prevent UITableViewCells from dequeue and reload

I am currently making an app which displays the user's profile. For that purpose, I used an UITableViewCell with custom cells for the different types of data (phone numbers, mail addresses, etc...). There is a maximum of 8 cells per profile.
The user is allowed to edit its profile in the simplest way. When the tableview's editing mode is triggered, all editable labels are replaced by textfields. And then turned back to labels when the modifications are finished.
Homever, there seems to be a problem with the cells that are not visible. Everytime they reappear in the view, they are reloaded, the setEditing:YES method is triggered again, etc... Because of this, every change made in the textfield is lost.
Is there a way to prevent the tableview to remove the non-visible cells and to add them back ? There are only eight cells, so it wouldn't be very resources consuming, and I wouldn't have to save their state everytime a change is made.
PS : I have tried several things with the dequeueReusableCellWithIdentifier method and the identifiers of each cell, but I have not managed to achieve what I want. Everytime I hide a cell, its content is refreshed.
You should use static cell not dynamic. Select table view and change config like image.
And add cell in interface builder!
In this case, you are not helped with UITableView`s Reusability(Reusability is ofcourse a great thing in most cases) but will have too much difficulty in preserving edits. So you can avoid reusability and prepare your cells before hand.
Add an NSMutableArray iVar or property in your ViewController
#property (nonatomic, strong) NSMutableArray *cells;
In your viewDidLoad: prepare your cells without any reuseIdentifier
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//Creates tableView cells.
[self createCells];
}
- (void)createCells
{
self.cells = [NSMutableArray array];
TCTimeCell *cellCallTime = [[TCTimeCell alloc] initWithTitle:#"CALL" forTimecard:_timecard andTimeEntryType:TCTimeEntryTypeCall];
[_cells addObject:cellCallTime];
TCTimeCell *cellLunchOut = [[TCTimeCell alloc] initWithTitle:#"LUNCH START" forTimecard:_timecard andTimeEntryType:TCTimeEntryTypeLunchOut];
[_cells addObject:cellLunchOut];
TCTimeCell *cellLunchIn = [[TCTimeCell alloc] initWithTitle:#"LUNCH END" forTimecard:_timecard andTimeEntryType:TCTimeEntryTypeLunchIn];
[_cells addObject:cellLunchIn];
TCTimeCell *cellSecondMealOut = [[TCTimeCell alloc] initWithTitle:#"2ND MEAL START" forTimecard:_timecard andTimeEntryType:TCTimeEntryTypeSecondMealOut];
[_cells addObject:cellSecondMealOut];
TCTimeCell *cellSecondMealIn = [[TCTimeCell alloc] initWithTitle:#"2ND MEAL END" forTimecard:_timecard andTimeEntryType:TCTimeEntryTypeSecondMealIn];
[_cells addObject:cellSecondMealIn];
TCTimeCell *cellWrapTime = [[TCTimeCell alloc] initWithTitle:#"WRAP" forTimecard:_timecard andTimeEntryType:TCTimeEntryTypeWrap];
[_cells addObject:cellWrapTime];
}
You can populate your tableView from this array.
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.cells.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
return self.cells[indexPath.row];
}
If you have a sectioned tableView, you can prepare your cells as array of arrays. In that case, your Data Source methods should look like below
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
return [self.cells count];
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.cells[section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
return self.cells[indexPath.section][indexPath.row];
}

UITableView tapping index doesnt scroll to section

I have a tableView where I have implemented section headers and I have the section titles appearing along the right hand side. However, when I tap the index it doesn't scroll to that section even though I can see in my logs that it is returning the correct section.
Here is my method for tableView:sectionForSectionIndexTitles:atIndex
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
NSLog(#"Title: %#", title);
NSLog(#"index: %d", index);
return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}
Any help would be great, thanks.
If you want to create alphabets scroll. This will helps.
-(void)viewDidLoad {
//section index in tableview
alphabetsArray = [[NSArray alloc] initWithObjects:#"#",#"A",#"B",#"C",#"D",#"E",#"F",#"G",#"H",#"I",#"J",#"K",#"L",#"M",#"N",#"O",#"P",#"Q",#"R",#"S",#"T",#"U",#"V",#"W",#"X",#"Y",#"Z", nil];
}
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return alphabetsArray;
}

Scrolling with indexbar in a tableview

I got a table view with approx 45k items. I want to use the indexbar to be able to scroll faster through my list.
Now I've looked at other questions on here, but I just can't figure out what to do.
I've got the following two functions:
-(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return alphabetsArray;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return index;
}
I don't understand what I should do next. Like, I have an array called airports which is sorted alphabetically. What should I be doing with this array?
update
so I found an article I could use and I'm pretty close now, I think. My table view only shows 2 items now though.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"ROWS METHOD CALLED");
NSArray *unsortedKeys = [alphabetizedAirports allKeys];
NSArray *sortedKeys = [unsortedKeys sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
NSString *key = [sortedKeys objectAtIndex:section];
NSArray *airportsForSection = [alphabetizedAirports objectForKey:key];
return [airportsForSection count];
}
I have about 60 items in my sortedKeys array. The airportsForSection array only gets 2 items though so when it returns the array, the tableview only shows 2 items. Any ideas how to solve?

UITableView reloadData doesn't work

I've read all similar questions and tried all suggestions, still nothing. Maybe someone can spot my flaw.
My view controller is initiated from another view controller, by one of two buttons. Button taps send NSNotification (with attached arrays), and this view controller anticipates this notification and then calls this method:
- (void)addContentToArray:(NSNotification *)aNotification {
array = [NSArray arrayWithArray:[aNotification object]];
([array count] == 6) ? (category = YES) : (category = NO);
[myTableView reloadData];
NSLog(#"%d", [array count]);
NSLog(#"%#", myTableView);
}
The method gets called every time, I can see that from changing array count. Here notification object is the array passed from previous view controller, and I assign these objects to my local array property - this is my UITableView source. So what I do is I try to reuse the UITableView to display elements of whatever array is being passed. And it works nicely for the first array passed (whichever first).
When I tap the second button, the new array is passed successfully (as mentioned before, I know that from log of [array count] which is different: 3 vs 6 objects in different arrays). However, what is not happening is that UITableView does not refresh (although the values passed when I select a row in the table are from the correct arrays, even though wrong values are displayed).
Here are UITableView data source methods:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Identifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"Identifier"];
}
cell.textLabel.text = [[array objectAtIndex:indexPath.row] objectForKey:#"name"];
if (category) {
cell.detailTextLabel.text = [[array objectAtIndex:indexPath.row] objectForKey:#"description"];
}
return cell;
}
So, what am I doing wrong?
A few other considerations that might help:
NSLog(#"%#", myTableView); returns (null), which is a bit worrying. myTableView here is UITableView from my nib file, which is correctly connected to the view controller, declared as property and synthesized
The view controller in question is a rightViewController of the PKRevealController, so when it is called repeatedly, viewWillAppear method is called, but not viewDidLoad (although, as I already mentioned, addContentToArray: method is being called every time as well)
Also, for those somewhat familiar with PKRevealController - when I try and log focusedController from my view controller, it says that frontViewController - the one that moves to reveal my view controller - is the one that is focused. Can that be the reason why myTableView is (null)?
I'd be grateful for any insight and help!
Need more code, that part where you created and call Myviewcontroller's addContentToArray method.
I think you used release code there for Myviewcontroller's object, try once with hide that part.
I managed to solve the issue by editing initWithNibName method (old line commented out)
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
//self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
self = [super initWithNibName:#"PurposeCategoryViewController" bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
Apparently, it did have something to do with the fact that my view controller (called PurposeCategoryViewController) was not the top/focused view controller in PKRevealController hierarchy. So, I just needed to specifically indicate my nib file.
What value are you returning in this delegate method:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
make sure it is 1

ios6 - numberOfRowsInSection doesn't run for custom tableviewcell, delegate/datasource set

I have a custom view EMViewController with a tableview as a subview. I want EMViewController to double as the controller for the subview, so I have it set as the delegate and dataSource.
// EMViewController.h
#interface EMViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
...
// EMViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.eTableView.delegate = self;
self.eTableView.dataSource = self;
}
I'm populating the data with an asynchronous array/dictionary from Facebook, so once I have the data, I run
[self.eTableView reloadData]
For the UITableViewDataSource protocol methods, I implemented the following:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(#"returns sections 0"); // This appears in my log
return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"returning count %d", [self.eList count]); // never shows up
return [self.eList count];
}
- (EMTableViewCell *)tableView:(EMTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// configure the cell
NSLog(#"index: %d", indexPath); // never shows up
...
}
So far, I have already tried:
making sure my storyboard is connected up
checking/rechecking the delegate/dataSource being set in viewDidLoad
completely restarting/clean-building my app just in case
creating a placeholder UIView subclass (although I'm not changing much)
creating a customUITableViewCell class with outlets for my custom layout (images, 3 labels)
testing out strong/weak references to the tableView outlet
Any advice would be appreciated. I think my problem is similar to this question, except I'm trying to add the tableview as a subview. My end goal is to populate the cells with a custom format that includes an image and three labels. Thanks!
Return 1 section instead of returning 0 sections
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

Resources