Static Cells - Hiding using the heightforRowatIndexPath method - ios

Hi guy I'm trying to hide some of the rows in section 1 (Second section) depending on what type of feedback the user has selected:
I'm using static cells but at the moment nothing is being removed when I select one of the options in the TypeVC. There are no errors at the moment but having a guess I think its something to do with the logical operators I'm using in the switch statement. Sorry for dumping my code but as I'm very new to IOS I don't know what exactly you guys would need to see.
[1]
if (variable == (1|2|3)){}
I'm used to Java and I use this kind statement quite frequently as it saves writing. Is this how to do it in objective-c?
[2]
Where and how have I gone wrong here trying to get the cells to dissapear?
FeedbackTableViewController:
#import "FeedbackTableViewController.h"
#import "TypeTableViewController.h"
#interface FeedbackTableViewController ()
#property NSInteger index;
#end
#implementation FeedbackTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidAppear:(BOOL)animated{
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (NSIndexPath *) tableView:(UITableView *)tableView
willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Type: %i",_type);
if (indexPath.section == 0 && indexPath.row == 0)
[self performSegueWithIdentifier:#"showTypeVC" sender:self];
return indexPath;
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
TypeTableViewController *tvc = [segue destinationViewController];
tvc.indexchoosen = _index;
}
//- (UITableViewCell *)tableView:(UITableView *)tableView
// cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//
// UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
//
// if (indexPath.row==0) cell.textLabel.text = _typeString;
// else if (indexPath.row)
//
// return cell;
//}
- (CGFloat) tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(#"section: %i row:%i",indexPath.section, indexPath.row);
if (indexPath.section == 1) {
switch (_type) {
case 0:
if (indexPath.row==(2|3|4))return 0;
break;
case 1:
if (indexPath.row==(0|1|4))return 0;
break;
case 2:
if (indexPath.row==(0|1|2|3))return 0;
break;
case 3:
return 0;
break;
case 4:
return 0;
break;
case 5:
return 0;
break;
default:
return 0;
break;
}
}
return 43;
}
- (IBAction)unwindtypeVC:(UIStoryboardSegue *)segue { }
#end
TypeTableViewController:
#import "TypeTableViewController.h"
#import "FeedbackTableViewController.h"
#interface TypeTableViewController ()
#end
#implementation TypeTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_typeoptions = #[#"Routing Issues",
#"Wrongly Labelled Location",
#"Missing Location",
#"Comment on Useability",
#"Suggestions",
#"General Feedback"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 6;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.textLabel.text = self.typeoptions[indexPath.row];
return cell;
}
- (NSIndexPath *)tableView:(UITableView *)tableView
willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
_indexchoosen = indexPath.row;
return indexPath;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
NSString *string = _typeoptions[_indexchoosen];
FeedbackTableViewController *fvc1 = [segue destinationViewController];
fvc1.typeString.text = _typeoptions[_indexchoosen];
fvc1.type = _indexchoosen;
}
#end
I'm open to better ideas on achieving what I want to achieve also so if you would consider telling me a more efficient way of doing this I would be grateful. I know delegates might be an option however I'm not confident with them yet and would thought this would be easier for me.

For [1], try this and see it yourself:
int test = 3;
if(test == (1 | 2))
NSLog(#"_MEH_");
Since it's bitwise OR operation, 0010 | 0001 equals to 0011, which is equal to 3. Hence, I would not advise you to use an operation like that. (If that's not intentional, of course).
For [2], you should use deleteRowsAtIndexPaths:withRowAnimation: call for UITableView in order to delete rows.
For example;
[self.tableView beginUpdates];
NSIndexPath* rowToDelete = [NSIndexPath indexPathForRow:0 inSection:0]; // For showing purposes only.
NSArray* indexArray = [NSArray arrayWithObjects:rowToDelete, nil];
[self.tableView deleteRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationMiddle];
[self.tableView endUpdates];
Also, don't forget to update your data source accordingly. You may want to remove some objects from
self.typeoptions
array.
P.S: Another addition, you should also change tableView:numberOfRowsInSection: since there will be less rows than 6.

I actually managed to use this method of changing the row heights to 0.
In order to do it effectively I had to to remove the placeholder/any initial text in the rows that I didn't want shown. This required some storyboard connections which you will see named as _feedbackText _startLocation etc. When a user selected a new row, they would perform a segue to the original feedback form and therefore the viewDidAppear was called. I used this to call the [self.tableView reloadData]. Originally the change in the variable _type would not actually change anything but the heightForRowAtIndexPath is recalled when the data is reloaded.
I'm sure that using the delete row at indexPath would have worked also but I wanted to store the information that the user may have typed before they changed the type of feedback.
The New Method:
- (CGFloat) tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSInteger i = indexPath.row;
if (indexPath.section == 1) {
switch (_type) {
case 0:
_startLocation.placeholder = #"Start Location:";
_destination.placeholder = #"Destination:";
_locationName.placeholder = #"";
_correctName.placeholder = #"";
_missingLocation.placeholder = #"";
if (i==2||i==3||i==4) return 0;
break;
case 1:
_startLocation.placeholder = #"";
_destination.placeholder = #"";
_locationName.placeholder = #"Location Name:";
_correctName.placeholder = #"Correct Name:";
_missingLocation.placeholder = #"";
if (i==0||i==1||i==4)return 0;
break;
case 2:
_startLocation.placeholder = #"";
_destination.placeholder = #"";
_locationName.placeholder = #"";
_correctName.placeholder = #"";
_missingLocation.placeholder = #"Missing Location:";
if (i==0||i==1||i==2||i==3)return 0;
break;
case 3:
return 0;
break;
case 4:
return 0;
break;
case 5:
return 0;
break;
default:
_startLocation.placeholder = #"";
_destination.placeholder = #"";
_locationName.placeholder = #"";
_correctName.placeholder = #"";
_missingLocation.placeholder = #"";
if (i==0||i==1||i==2||i==3||i==4)return 0;
break;
}
} else if (indexPath.section==2 && indexPath.row==2) return 240;
else if (indexPath.section==0 && indexPath.row==0) return 40;
return 30;
}
This will essentially hide but not get rid of the information in the text fields. This is very useful if you want to the keep any information the user typed in.
I hope this helps anyone trying to hide rows in a grouped static table view controller.

Related

TableView sections don't seem to work Objective - C

The problem I am encountering is that I don't know how to code my tableViewController properly, so it displays 2 sections, one with Items (object type) which value is higher than 50, and second section for Items valued less than 50. My code looks like this:
#implementation ItemsViewController
- (instancetype)init
{
self = [super initWithStyle:UITableViewStylePlain];
if(self)
{
for (int i = 0; i < 5; i++)
{
[[ItemStore sharedStore] createItem];
}
}
return self;
}
- (instancetype)initWithStyle:(UITableViewStyle)style
{
return [super initWithStyle:UITableViewStylePlain];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"UITableViewCell"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView;
{
return #[#">50", #"<=50"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
{
return [[[ItemStore sharedStore] itemsValuedOver50] count];
}
else
{
return [[[ItemStore sharedStore] itemsValuedBelowOrEqual50] count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"UITableViewCell" forIndexPath:indexPath];
if (indexPath.section == 0)
{
NSArray *items = [[ItemStore sharedStore] itemsValuedOver50];
NSArray *item = items[indexPath.row];
cell.textLabel.text = [item description];
}
else
{
NSArray *items = [[ItemStore sharedStore] itemsValuedBelowOrEqual50];
NSArray *item = items[indexPath.row];
cell.textLabel.text = [item description];
}
return cell;
}
#end
Section names (">50" and <=50) appear in the middle of the right side of the screen(looks like it is out of the table view), and the table view still behaves as it had only one section. These 5 items are not sorted in any way and I have no idea why.
Here is the link to screenshot of the application: http://imgur.com/3MoHmmn
I've been looking for answer for some time, but to be fair i don't know how to describe my problem well enough to find any solution online, thats why I'm creating a new topic.
Thanks for your help in advance.
EDIT: I've figured out the problem. It took me few days and it was preety stupid. I used wrong method. Instead of using sectionIndexTitlesForTableView: i should have used titleForHeaderInSection:. Now everything works as i wanted it to work.
Try setting the section height via the delegate
- (CGFloat)tableView:(UITableView *)tableView heightForSection:(NSUInteger)section;

Trying to add sections with headers to UITableViewController (iOS)

I'm still very new to objective-c, and I've looked all for an answer. I haven't been able to successfully implement anything I've found yet. I'm using an open source sliding menu that I found, and I haven't found anything that fits within the code it uses. (I'd post a link to it, but I can't remember where I found it.)
EDIT: I've got the sections added. Now, I just need to make it so the first cell in the second section only shows up in the first section. http://i.imgur.com/gSIRNJ3.png
I've updated the code below. Here's a reference of what I'm trying to do. Right now, cell 1 shows up in both section 1 and section 2.
SECTION 1: (no header)
- cell 1: "Search Nouns"
SECTION 2: "Noun Conjugation"
- cell 2: "Regular Nouns"
- cell 3: "Irregular Nouns"
- cell 4: "Uncountable Nouns"
#import "BaseTableViewController.h"
#import "KYSlideMenuViewController.h"
#interface BaseTableViewController () {
int _currentCtrNum;
}
#end
#implementation BaseTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// 最初に表示するビューを設定する
_currentCtrNum = -1;
NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:0];
[self tableView:self.tableView didSelectRowAtIndexPath:path];
}
#pragma mark -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2 ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section == 0)
return 1;
else
return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if(section==0)
return 0;
else
return 25;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0)
return #"";
else
return #"reference";
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
switch (indexPath.row) {
case 0:
cell.textLabel.text = #"名詞検索";
break;
case 1:
cell.textLabel.text = #"Side B";
break;
case 2:
cell.textLabel.text = #"Side C";
break;
case 3:
cell.textLabel.text = #"Side D";
default:
break;
}
cell.backgroundColor = [UIColor grayColor];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
for (int i = 0 ; i < [tableView numberOfRowsInSection:0] ; i++) {
NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];
if (i == indexPath.row) {
cell.textLabel.textColor = [UIColor whiteColor];
} else {
cell.textLabel.textColor = [UIColor blackColor];
}
}
// 現在のビューコントローラなら表示するだけ
if (_currentCtrNum == indexPath.row) {
[(KYSlideMenuViewController *)self.parentViewController slideChildTo:KYS_CLOSE];
return;
}
_currentCtrNum = (int)indexPath.row;
// 新しいビューコントローラを生成してコンテナのビューコントローラを入れ替え
UIViewController *toCtr;
switch (indexPath.row) {
case 0: // noun search
toCtr = [[self storyboard] instantiateViewControllerWithIdentifier:#"itemTableView"];
break;
case 1: // navigationあり
toCtr = [[self storyboard] instantiateViewControllerWithIdentifier:#"mainView"];
break;
case 2: // navigationなし
toCtr = [[self storyboard] instantiateViewControllerWithIdentifier:#"subView"];
break;
case 3: // navigationあり
toCtr = [[self storyboard] instantiateViewControllerWithIdentifier:#"srdView"];
break;
default:
return;
}
[(KYSlideMenuViewController *)self.parentViewController changeTo:toCtr];
}
#end
There also supposed to be a delegate for numberOfSectionsInTableView: where you would return 2;

iOS UITableView reusable cell slow when showing all of them

I have a table view which has 10000+ cells. and there is a segment button (All/Favorite) on the top.
this is the call back for the segment:
- (IBAction)call_segment:(id)sender {
[self.tableView beginUpdates];
[self.tableView reloadData];
[self.tableView endUpdates];
}
for favorite page, even when there are no favorite items, I simply set the cell height to be 0. But in this way, I created all 10000+ cells on screen.
if 'all' is selected, the table works just fine since cells have normal height and only some of them are dequeued on screen.
Here is my code:
//if it's not in favorite, just hide it by setting the height to be 0
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self isFavorite]) {
int uniqueId = [self uniqueIdWithIndexPath:indexPath];
if ([DATABASE isFavoriteWithMode:self.mode uniqueId:uniqueId] == NO) {
return 0;
}
}
return 60;
}
//in table view datasource:
//I think the problem is, when setting the height to be 0, all the cells are allocated. I set the cell to be hidden but still takes memory. any way to deal with it?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
BOOL isFavorite = [DATABASE isFavoriteWithMode:self.mode uniqueId:[self uniqueIdWithIndexPath:indexPath]];
if ([self isFavorite] && isFavorite == NO) {
cell.hidden = YES;
return [[UITableViewCell alloc] init];
}
else {
cell.hidden = NO;
ListCell *cell = (ListCell *)[tableView dequeueReusableCellWithIdentifier:CELL_LIST];
Datum *datum = [DATABASE datumWithMode:self.mode uniqueId:[self uniqueIdWithIndexPath:indexPath]];
BOOL isRead = [DATABASE isReadWithMode:self.mode uniqueId:[self uniqueIdWithIndexPath:indexPath]];
cell.indexLabel.text = [NSString stringWithFormat:#"%d", datum.uniqueId];
cell.titleLabel.text = [NSString stringWithFormat:#"%#", datum.q];
return cell;
}
}
Note: I dont wanna just show the favorite cells, since the logic is way too complex. I am using sqlite, but i dont think database performance is the problem, since the 'all' tab works just fine.
The reason i wanted to just set the height to be 0 is the simple implementation of cell numbers
- (BOOL)isFavorite {
return self.segment.selectedSegmentIndex == 1;
}
- (IBAction)call_segment:(id)sender {
[self.tableView beginUpdates];
[self.tableView reloadData];
[self.tableView endUpdates];
}
#define NUM_SECTIONS 15
- (int)numRows {
return [DATABASE numberOfDataForModes:self.mode];
}
- (int)numSections {
if ([self numRows] % NUM_SECTIONS > 0) {
int numSections = [self numRows] / [self numRowsPerSection];
if ([self numRows] % [self numRowsPerSection] > 0) {
numSections++;
}
return numSections;
}
return NUM_SECTIONS;
}
- (int)numRowsPerSection {
return [self numRows] / NUM_SECTIONS;
}
- (int)numRowsInLastSection {
if ([self numRows] % ([self numSections] - 1) > 0) {
return [self numRows] % ([self numSections] - 1);
}
else {
return [self numRowsPerSection];
}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
int start = section * [self numRowsPerSection] + 1;
int end = start + [self numRowsPerSection] - 1;
if (end > [self numRows]) {
end = [self numRows];
}
return [NSString stringWithFormat:#"From %d to %d", start, end];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
NSMutableArray *titles = [NSMutableArray arrayWithCapacity:[self numSections]];
int start = 1;
while (start < [self numRows]) {
NSString *title = [NSString stringWithFormat:#"%d", start];
[titles addObject:title];
start += [self numRowsPerSection];
}
return titles;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return index;
}
- (int)uniqueIdWithIndexPath:(NSIndexPath *)indexPath {
int uniqueId = indexPath.row + 1 + indexPath.section * [self numRowsPerSection];
return uniqueId;
}
- (NSIndexPath *)indexPathWithUniqueId: (int)uniqueId {
int section = (uniqueId - 1) / [self numRowsPerSection];
int row = uniqueId - 1 - [self numRowsPerSection] * section;
return [NSIndexPath indexPathForRow:row inSection:section];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self isFavorite]) {
int uniqueId = [self uniqueIdWithIndexPath:indexPath];
if ([DATABASE isFavoriteWithMode:self.mode uniqueId:uniqueId] == NO) {
return 0;
}
}
return 60;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == [self numSections] - 1) {
return [self numRowsInLastSection];
}
return [self numRowsPerSection];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [self numSections];
}
Instead of hiding the cells why dont you just return 0 from the datasource method
– tableView:numberOfRowsInSection:
You can just make use of the isFavorite value within this function and return 0 if there it is NO.
You got it already. The problem is the size of 0 of non-favorite cells. That contradicts the idea of reusabel cells. You will have thousands of cells on the screen, although invisible but existing and therefore resource consuming. Better think of a smarter way of doing that. Your data source delegate (view controller I guess) should only return the number of non-fav cells and therefore cellForRowAtIndexPath should only provide those cells of non-fav items. Plus cellForRowAtIndexPath should actually reuse the cells which I do not see in your code sniplet.
No matter how much you try having 10,000 views onscreen is not going to be the solution to your problem. You need to change your code structure such that you can return 0 for the tableView:numberOfRowsInSection: delegate when the favourites tab is chosen.
Any other 'solution' is an attempt to hack an alternative together, but this will not work and is bad code practice anyway. Implement it properly, by responding to the delegates properly.
I've given up making both table section separated. the logic is way too complicated.
I guess there is no way to save memory even when you hide the cells. Thank you guys for your input. you are all correct.
It's actually not that bad since favorite table are typically not that long. just one section with all entries.

iOS 6 UITextField inside UITableViewCell with sections get wrong data on scrolling

So basically I am getting the wrong data on every textField by scrolling my TableView (Everything is mixed up). I know, this is because the awesome optimization of the reusable cells in the UITableView, so the indexpath are changing constantly, but still I don't know how to solve this.
Having an UILabel in the same cell I solved this problem by adding a specific tag in every label view, so the tableview knows which data is going to return on a view but for some reason this is not possible for my textfield view.
Here is my code:
#define DESCRIPTION_TAG 10
#define TEXTFIELD_TAG 11
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Description Cell";
// Intatiating my own TableViewCell
CustomLocationCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (!cell){
cell = [[CustomLocationCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.rowText = (UILabel *) [cell viewWithTag:DESCRIPTION_TAG]; // returning view with unique tag
cell.rowTextField = (UITextField *)[cell viewWithTag:TEXTFIELD_TAG]; // NOT WORKING!!! Data get mix up
cell.rowTextField.delegate = cell; // sending the delegate to custom class cell
cell.delegate = self; // make connection with my #protocol
switch (indexPath.section) {
case 0:
cell.rowText.text = [self.descriptions objectAtIndex:indexPath.row];
cell.rowTextField.placeholder = [self.descriptionsPlaceholder objectAtIndex:indexPath.row];
break;
case 1:
cell.rowText.text = [self.rooms objectAtIndex:indexPath.row];
cell.rowTextField.placeholder = [self.contentPlaceholder objectAtIndex:indexPath.row];
break;
default:
break;
}
return cell;
}
Any help is appreciated.
Here is an update of my data source:
On my CustomTableViewCell I do have outlets for my UILabel and UITextfield
// UITableView DataSource
- (NSArray *)sections
{
if (!_sections){
_sections = [[NSArray alloc] initWithObjects:#"First Description", #"Second Descriptions", nil];
}
return _sections;
}
- (NSMutableArray *)rooms
{
if (!_rooms){
_rooms = [[NSMutableArray alloc] init];
}
return _rooms;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.sections.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section) {
case 0:
return [self.descriptions count];
break;
case 1:
return [self.rooms count];
break;
default:
break;
}
return 0;
}
I'm not sure exactly what you're trying to accomplish, but here's an example that works with labels and text fields with placeholder text. You'll notice that in cellForRowAtIndexPath, I set both the text and placeholder to nil, so that when a cell is reused, it doesn't have the wrong text in it. Also, when doing the text fields, I check if the array has placeholder text at that index, and if so, I reset the placeholder, and if not I reset the text.
In order for the text field delegate methods to work properly, each text filed needs a unique tag (not just one tag for labels and another for text fields) that I set equal to (indexPath.row + 1000*indexPath.section).
#import "TableController.h"
#import "CustomLocationCell.h"
#interface TableController ()
#property (strong,nonatomic) NSArray *descriptions;
#property (strong,nonatomic) NSMutableArray *descriptionsPlaceholder;
#property (strong,nonatomic) NSArray *rooms;
#property (strong,nonatomic) NSMutableArray *contentPlaceholder;
#end
#implementation TableController
- (void)viewDidLoad {
[super viewDidLoad];
self.descriptions = #[#"One",#"Two",#"Three",#"Four",#"Five",#"Six",#"Seven",#"Eight"];
self.descriptionsPlaceholder = [#[#"Placeholder1",#"Placeholder2",#"Placeholder3",#"Placeholder4",#"Placeholder5",#"Placeholder6",#"Placeholder7",#"Placeholder8"] mutableCopy];
self.rooms = #[#"Room1", #"Room2", #"Room3", #"Room4",#"Room5", #"Room6", #"Room7", #"Room8"];
self.contentPlaceholder = [#[#"ContentPH1",#"ContentPH2",#"ContentPH3",#"ContentPH4",#"ContentPH5",#"ContentPH6",#"ContentPH7",#"ContentPH8"] mutableCopy];
[self.tableView reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.descriptions.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Description Cell";
CustomLocationCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.rowTextField.delegate = self;
cell.rowTextField.tag = indexPath.row + 1000*indexPath.section;
cell.rowTextField.placeholder = nil;
cell.rowTextField.text = nil;
switch (indexPath.section) {
case 0:
cell.rowText.text = [self.descriptions objectAtIndex:indexPath.row];
if ([self.descriptionsPlaceholder[indexPath.row] hasPrefix:#"Placeholder"]) {
cell.rowTextField.placeholder = [self.descriptionsPlaceholder objectAtIndex:indexPath.row];
}else{
cell.rowTextField.text = [self.descriptionsPlaceholder objectAtIndex:indexPath.row];
}
break;
case 1:
cell.rowText.text = [self.rooms objectAtIndex:indexPath.row];
if ([self.contentPlaceholder[indexPath.row] hasPrefix:#"ContentPH"]) {
cell.rowTextField.placeholder = [self.contentPlaceholder objectAtIndex:indexPath.row];
}else{
cell.rowTextField.text = [self.contentPlaceholder objectAtIndex:indexPath.row];
}
break;
default:
break;
}
return cell;
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
if (textField.tag >= 1000 && ![textField.text isEqualToString: #""]) {
[self.contentPlaceholder replaceObjectAtIndex:textField.tag - 1000 withObject:textField.text];
}else if (textField.tag < 1000 && ![textField.text isEqualToString: #""]) {
[self.descriptionsPlaceholder replaceObjectAtIndex:textField.tag withObject:textField.text];
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}

Why does Section data reset each time?

I'm quite new to iOS development (as you'll see from my code below). I like to help myself learn new languages by manipulating existing code to do something different. I've hit a bit of a blank with this one though. At the end of each section in a table view, the data it is using resets and it starts again, instead of carrying on. Can anyone tell me where the fault lies here please?
#import "RootViewController.h"
#import "DataController.h"
#import "DetailViewController.h"
#import "Play.h"
#implementation RootViewController
#synthesize dataController;
#synthesize play;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
self.title = NSLocalizedString(#"Plays", #"Master view navigation title");
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Only one section.
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
/*
The number of rows varies by section.
*/
NSInteger rows = 0;
switch (section) {
case 0:
rows = 3;
break;
case 1:
rows = 1;
break;
case 2:
rows = 2;
break;
default:
break;
}
return rows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath {
static NSString *CellIdentifier = #"PlayCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Get the object to display and set the value in the cell.
Play *playAtIndex = [dataController objectInListAtIndex:indexPath.row];
cell.textLabel.text = playAtIndex.title;
return cell;
}
// Section header titles
#pragma mark -
#pragma mark Section header titles
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:
(NSInteger)section {
NSString *secttitle = nil;
switch (section) {
case 0:
secttitle = NSLocalizedString(#"Comedy", #"Comedy section title");
break;
case 1:
secttitle = NSLocalizedString(#"Action", #"Action section title");
break;
case 2:
secttitle = NSLocalizedString(#"Drama", #"Drama section title");
break;
default:
break;
}
return secttitle;
}
// End of section header titles
#pragma mark -
#pragma mark Table view selection
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
/*
When a row is selected, the segue creates the detail view controller as the destination.
Set the detail view controller's detail item to the item associated with the selected
row.
*/
if ([[segue identifier] isEqualToString:#"ShowSelectedPlay"]) {
NSIndexPath *selectedRowIndex = [self.tableView indexPathForSelectedRow];
DetailViewController *detailViewController = [segue destinationViewController];
detailViewController.play = [dataController objectInListAtIndex:selectedRowIndex.row];
}
}
#end
Thanks for your replies so far. The Detail view works fine, and judging by your feedback so far it appears it's because this code isn't being evaluated, but I can't seem to work out where to incorporate it in the master view.
NSString *cellText = nil;
switch (indexPath.section) {
case 0:
cellText = play.date;
break;
case 1:
cellText = play.genre;
break;
case 2:
cellText = [play.characters objectAtIndex:indexPath.row];
break;
default:
break;
}
cell.textLabel.text = cellText;
return cell;
Because you're only using the indexPath.row to get your cell content. Your data model should also consider the indexPath.section:
Play *playAtIndex = [dataController objectInListAtIndex:indexPath.row];
cell.textLabel.text = playAtIndex.title;
i.e. you have a one-dimensional data model...
This line:
Play *playAtIndex = [dataController objectInListAtIndex:indexPath.row];
only takes into account the current indexPath's row, which also resets to zero at the beginning of each section. You'll need to use an array of arrays or otherwise account for the rows in previous sections to get the correct index.

Resources