I got 3 labels on my view displaying some stuff, a table view below (beginning at screen halfway down) and
a data retrieval method where I fill up an NSArray for the table view.
Everything works, the problem now is if there are more than a few elements in the array for the table view (more than the screen can display at once) I'm not able to scroll down to the bottom, it's always autoscrolling up again, as if the table view was empty space there (which it is not, I checked of course).
It worked on the previous screen the way I did it, I think the iOS doesn't notice the labels or something similar - it thinks the table view has the whole screen for itself. I tried resizing the table view in the xib to let it end just before the screen ends but the table view occupies all screen beginning from where it started. I also tried to
initWithFrame:CGRectMake...
But this did not work as well. Any ideas whats going on here? I'd be really glad about some help, thanks guys!
EDIT:
.h file
#interface ZProjekt : UIViewController <RetailHeaderTabSource, MFMailComposeViewControllerDelegate , UITableViewDelegate, UISearchDisplayDelegate, UISearchBarDelegate, UITableViewDataSource>
{
NSString *messageBody;
NSString *subjectBody;
NSMutableArray *fieldsInTable;
int fieldscounter;
}
#property (strong, nonatomic) IBOutlet UILabel *tableNamed;
#property (strong, nonatomic) IBOutlet UILabel *objectsInTable;
#property (strong, nonatomic) NSArray *DetailModal;
#property (strong, nonatomic) IBOutlet UILabel *mboSyncGroup;
#property (strong, nonatomic) IBOutlet UITableView *tableView;
#end
here is the code of the tableview .m file
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return fieldscounter;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = fieldsInTable[indexPath.row];
[Constants updateLabelValues:cell.textLabel withKey:k_IPhone_DetailCell_HeaderLabel];
return cell;
}
Try this one :
[tableView setContentOffset:CGPointMake(tableView.frame.size.width, tableView.frame.size.height) animated:NO];
or
on the TableViewController->Table View check the clip Subviews n Autoresize Subviews.
Also,
make sure the total rows are the same with the total contents in the tableView which is 3
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 3;
}
Problem Found ! The problem is, the tableview doesn't know which table you are scrolling either the last one or the previous one. You need to re-assign the value to the NSMutableArray. You need to store the content of the array in the table1 into array or dictionary and you need to reassign in the CellForRowIndexPath. I had the same problem like this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView != table2.tblView) {
fieldsInTable = table1.fieldsInTable;
}
else
{
fieldsInTable = table2.fieldsInTable;
}
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = fieldsInTable[indexPath.row];
[Constants updateLabelValues:cell.textLabel withKey:k_IPhone_DetailCell_HeaderLabel];
return cell;
}
Related
I have a table with custom cells that are made from a nib file. When a user clicks a cell it expands in height , thus simulating a dropdown. In the custom cell there is an imageView that has a dropDown image when the cell is not clicked. However when the cell is clicked it should change the image to a collapse image or arrow up image to show that the cell is open.
I am having a problem changing the image when the cell expands from arrow down to arrow up and vice versa. I would like assistance achieving this.
Here is my code :
ViewController.h
#interface ViewController : UIViewController <UITableViewDelegate,
UITableViewDataSource>
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#property(nonatomic, strong) NSArray *items;
#property (nonatomic, assign) BOOL expandFlag;
#property (nonatomic, assign) NSIndexPath* selectedIndex;
ViewController.m
- (NSArray *) items
{
if (!_items) {
_items = [NSArray new];
}
return _items;
}
- (void)viewDidLoad {
[super viewDidLoad];
UINib *nib = [UINib nibWithNibName:#"CustomTableViewCell" bundle:nil];
[self.tableView registerNib:nib forCellReuseIdentifier:#"cell"];
_items = #[
#{#"title":#"Simpson", #"short":#"Homer", #"long":#"Mr jhvjm,b;k t tyfy rctrc rtcf rvty rthvgh bb r5ertvyg hg r5 tyhg 6ruygj 8rutyj r6yiugg tygdtjhgfdt hg tvt gthgfgni7yjftgjhb ftgfh b yjh gtfjfyhh j jj j", #"image":#"There are many ways to create expandable cells in the table view. Few of them you can easily find on this blog or somewhere in Google. One of that is the official Apple “Date cell” demo code. However, most of that describing the little hard way by using operations directly on constraints."},
#{#"title":#"Simpson", #"short":#"Marge", #"long":#"Mrs bjyvhm uikn o utv jb k", #"image":#"There are many ways to create expandable cells in the table view. Few of them you can easily find on this blog or somewhere in Google. One of that is the official Apple “Date cell” demo code. However, most of that describing the little hard way by using operations directly on constraints."},
#{#"title":#"Simpson", #"short":#"Bart", #"long":#"Mr vubj cbjknuy iubyuvjh biubkj ", #"image":#"There are many ways to create expandable cells in the table view. Few of them you can easily find on this blog or somewhere in Google. One of that is the official Apple “Date cell” demo code. However, most of that describing the little hard way by using operations directly on constraints."},
#{#"title":#"Simpson", #"short":#"Lisa", #"long":#"Miss jbjvjbbiuvu yuvhj uby ", #"image":#"There are many ways to create expandable cells in the table view. Few of them you can easily find on this blog or somewhere in Google. One of that is the official Apple “Date cell” demo code. However, most of that describing the little hard way by using operations directly on constraints."},
#{#"title":#"Simpson", #"short":#"Maggie", #"long":#"Miss iubniyujh k iuuh ", #"image":#"There are many ways to create expandable cells in the table view. Few of them you can easily find on this blog or somewhere in Google. One of that is the official Apple “Date cell” demo code. However, most of that describing the little hard way by using operations directly on constraints."},
#{#"title":#"Flanders", #"short":#"Ned", #"long":#"Mr hbuyvj iybkj nui uhc n", #"image":#"There are many ways to create expandable cells in the table view. Few of them you can easily find on this blog or somewhere in Google. One of that is the official Apple “Date cell” demo code. However, most of that describing the little hard way by using operations directly on constraints."}
];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) didExpandCell{
_expandFlag = !_expandFlag;
[self.tableView reloadRowsAtIndexPaths:#[_selectedIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.items.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
NSDictionary *item = _items[indexPath.row];
cell.titleImage.text = [item objectForKey:#"title"];
cell.longLabel.text = [item objectForKey:#"long"];
cell.shortLabel.text = [item objectForKey:#"image"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
_selectedIndex = indexPath;
[self didExpandCell];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (_expandFlag && _selectedIndex == indexPath) {
return 400;
}
return 200;
}
CustomeTableViewcell.h
#interface CustomTableViewCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UIImageView *thumbImage;
#property (weak, nonatomic) IBOutlet UILabel *titleImage;
#property (weak, nonatomic) IBOutlet UILabel *shortLabel;
#property (weak, nonatomic) IBOutlet UILabel *longLabel;
#property (weak, nonatomic) IBOutlet UIImageView *dropDownImage;
CustomerTableViewCell.m
- (void)awakeFromNib {
[super awakeFromNib];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
You should check and change dropDownImage in cellForRowAtIndexPath method. Something likes the code below
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
NSDictionary *item = _items[indexPath.row];
cell.titleImage.text = [item objectForKey:#"title"];
cell.longLabel.text = [item objectForKey:#"long"];
cell.shortLabel.text = [item objectForKey:#"image"];
NSString *dropDownImageName = [indexPath isEqual:_selectedIndex] && _expandFlag ? #"ARROW_DOWN" : #"ARROW_UP";
cell.dropDownImage.image = [UIImage imageNamed:dropDownImageName];
return cell;
}
Change ARROW_UP and ARROW_DOWN with your image names.
I have two UITableViews using Storyboards in Xcode 7. I've set the delegate and dataSource using the Connections Inspector for both table views.
Let the first table view be the main table view and let the table views within each cell of the main table view be the detail table views with cell identifiers named appropriately and respectively.
When [tableView dequeueReusableCellWithIdentifier:#"MainCell" forIndexPath:indexPath] executes, it immediately calls its dataSource method -cellForRowAtIndexPath: for DetailCell preventing me from setting a custom instance variable in time to add the appropriate data to each cell.
The following is a simplified example marked using comments.
MainTableViewController:
#implementation MainTableViewController
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Keep in mind the following two (2) lines are set using the Connections Inspector
//cell.detailTableView.dataSource = cell;
//cell.detailTableView.delegate = cell;
// Stepping over the following line will jump to the
// other `-cellForRowAtIndexPath:` (below) used to set
// the detail info.
cell = (MainTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"MainCell" forIndexPath:indexPath];
CustomObj *obj = self.mainData[indexPath.row];
cell.nameLabel.text = obj.name;
cell.additionalInfo = obj.additionalInfo; // This line is not set before instantiation begins for the detail table view...
return cell;
}
...
#end
DetailTableViewCell (contains a UITableView and implements appropriate protocols):
#interface DetailTableViewCell : UITableViewCell <UITableViewDataSource, UITableViewDelegate>
#property (nonatomic, weak) IBOutlet UILabel *nameLabel;
#property (nonatomic, weak) IBOutlet UITableView *detailTableView;
#property (nonatomic, strong) CustomObj *additionalInfo;
#end
#implementation DetailTableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell = (DetailTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"DetailCell" forIndexPath:indexPath];
// Instantiate detail ...
cell.detailLabel.text = self.additionalInfo.text;
// Problem!
// self.additionalInfo == nil thus we cannot set a value to the label.
return cell;
}
...
#end
The problem is when the detail -cellForRowAtIndexPath: method is called, I haven't had a chance to set a value for its dataSource, in this case, additionalInfo.
There are many possible ways to fix your problem, but first I would say that, your design seems not a good one, A UItableViewCell has another UITableView, and another UItableViewCell inside this UITableView? Why you do this? Just use one UITableView and put all of your views into one UItableViewCell as subViews should be enough.
Now get to your problem:
I would suggest not to use IBOutlet for setting up your delegate and dataSource, use code. This can give you a chance to delay setting the dataSource and delgate when you are ready. Once you think it's the proper time, just call [cell.detailTableView reloadData] will trigger your DetailTableViewCell to invoke cellForRowAtIndexPath
#implementation MainTableViewController
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Keep in mind the following two (2) lines are set using the Connections Inspector
//cell.detailTableView.dataSource = cell;
//cell.detailTableView.delegate = cell;
// Stepping over the following line will jump to the
// other `-cellForRowAtIndexPath:` (below) used to set
// the detail info.
cell = (MainTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"MainCell" forIndexPath:indexPath];
CustomObj *obj = self.mainData[indexPath.row];
cell.nameLabel.text = obj.name;
cell.additionalInfo = obj.additionalInfo; // This line is not set before instantiation begins for the detail table view...
// setup dataSource and delegate now
cell.detailTableView.dataSource = cell;
cell.detailTableView.delegate = cell;
// call reloadData whenever you think is proper
[cell.detailTableView reloadData];
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* cell = nil;
//Check this call is for which table view.
if(tableView == detailTableView) {
cell = (MainTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"MainCell" forIndexPath:indexPath];
// Do any additional setup you want with MainCell
} else {
cell = (DetailTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"DetailCell" forIndexPath:indexPath];
// Do any additional setup you want with DetailCell
}
return cell;
}
I have a UITableView containing custom cells. All works fine. But after, I decided to add a searchBar in order to... Search !
So, I added a "Search Bar and Search Display Controller" from the "Object Library" in my xib file, under the UITableView.
I created a specific class for the custom cell :
"CustomCell.h"
#class CustomCell;
#interface CustomCell : UITableViewCell
{
}
#property (weak, nonatomic) IBOutlet UILabel *lblDate;
#property (weak, nonatomic) IBOutlet UILabel *lblAuteur;
#end
"CustomCell.m"
no interesting stuff
"CustomCell.xib"
The "Event" class :
#interface Event : NSObject
{
}
#property (strong, nonatomic) NSString *desc;
#property (strong, nonatomic) NSString *dateCreation;
#end
And the view containing the UITableView and the UISearchBar :
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"cell";
// Determinate the current event
Event *currentEvent;
if (tableView == self.searchDisplayController.searchResultsTableView)
currentEvent = [filteredArray objectAtIndex:indexPath.row];
else
currentEvent = [notFilteredArray objectAtIndex:indexPath.row];
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil)
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.lblAuteur.text = [currentEvenement desc];
cell.lblDate.text = [currentEvenement dateCreation];
return cell;
}
Ok, now we can come to my problem. After loading the tableView, the custom cells are displaying well. But not if I use the SearchBar :
If there is an event with the desc attribute equal to "foo", and if I enter "bar" in the SearchBar, I obtain the "No results" message. It's normal.
If there is an event with the desc attribute equal to "foo", and if I enter "foo" in the SearchBar, the cells are displaying, but without their content ! I'm just seeing the cells' borders, and the lblDate and the lblAuteur are equal to nil.
Why have I this behaviour ? Thanks a lot for your help... I precise that the filteredArray and the notFilteredArray are correctly filled (I checked that many times). It means that the search mechanism is working well.
After much "searching" (pun) I've found the problem.
When you reference the tableview with the prototype cell you can't rely on the tableview that is passed in because sometimes that tableview is the search tableview without the prototype cell.
so instead of...
HomeTabCell *cell = (HomeTabCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
replace the "tableview" above with a direct connection to the table view with the prototype cell
HomeTabCell *cell = (HomeTabCell *)[self.HomeTableView dequeueReusableCellWithIdentifier:CellIdentifier];
Hope this helps!
Check these settings, if there not
self.searchDisplayController.searchResultsDelegate = self;
self.searchDisplayController.searchResultsDataSource = self;
try this,
Sorry for people searching to resolve this issue...
I could not fix it, and the prime contractor do not want this function anymore, so I gave up.
I am building an iOS app with a custom tab bar at the top. When i click on the home icon, I want it to show a tableview directly below the tab bar. Currently my code does just that, however the data doesnt show correctly. If I limit my Cells to 4 or less (in the numberFoRowsInSection), the data will show, if i have like say 15 cells, the data shows for a split second and then it disappears. I have been looking at tutorials all over the place and everything seems to be correct, the data will show correctly in a stand alone view(like i created a similar to a singleview application in the story board and made that the initial view). I dont know where i am wrong. Below is my code in implementation file of the tableviewContoller class:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 4;//this works... but change to 15 it doesnt work
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
PrayerCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CustomCell"];
if (cell == nil) {
NSLog(#"cell is Nil"); //never hits this
cell = [[PrayerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"CustomCell"];
}
//the data showing is the correct data that i want to see
cell.dName.text = [[news objectAtIndex:indexPath.row] objectForKey:#"displayname"];
cell.priority.text = [[news objectAtIndex:indexPath.row]objectForKey:#"priority"];
cell.dateTime.text = [[news objectAtIndex:indexPath.row] objectForKey:#"datetime"];
cell.numPraying.text = #"2 praying";
cell.requestPreview.text = [[news objectAtIndex:indexPath.row] objectForKey:#"request"];
NSLog(#"created cell") //verify cell was made
return cell;
}
Here is my PrayerCell.H file
#interface PrayerCell : UITableViewCell
#property (weak, nonatomic)IBOutlet UILabel *requestPreview;
#property (weak, nonatomic)IBOutlet UILabel *dName;
#property (weak, nonatomic)IBOutlet UILabel *dateTime;
#property (weak, nonatomic)IBOutlet UILabel *numPraying;
#property (weak, nonatomic)IBOutlet UILabel *priority;
#end
I made no Changes to the prayerCell.M file
if you need me to post any other code blocks/screenshots please let me know.
The cell should not be an ivar, it should be a local variable. You also need to dequeue the cell in your tableView:cellForRowAtIndexPath: method:
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:#"CustomCell"];
I'm a newbie programming iOS and I've a problem adding a new cell to a UITableView object. I'm using an storyboard and one of the scenes is a UIViewController that has several subviews: textfields, a tableview, etc. I intend to add rows to this tableView from a detail scene.
I'm able to initially add rows to the table, but I'm not able to add a row afterwards. When I press a button to add the row I call the method '[self.stepsListTableView reloadData];' which produces a call to the method '- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section' and it returns a correct value, including the new array element. But method '- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath' is not called to update the table.
I do not understand what I'm doing wrong.
Details of my source code:
WorkoutDetailsViewController.h
(…)
#interface WorkoutDetailsViewController : UIViewController <StepDetailsViewControllerDelegate, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource>
#property (nonatomic, weak) id <WorkoutDetailsViewControllerDelegate> delegate;
#property (nonatomic, strong) Workout *workout;
(…)
#property (nonatomic, retain) IBOutlet UITableView *stepsListTableView;
(…)
WorkoutDetailsViewController.m
(…)
#synthesize stepsListTableView;
(…)
- (void)viewDidLoad
{
[super viewDidLoad];
addButton.enabled = FALSE;
workoutNameField.delegate = self;
if (self.workout == nil) {
self.workout = [[Workout alloc] init];
self.stepsListTableView = [[UITableView alloc] init];
}
self.stepsListTableView.delegate = self;
self.stepsListTableView.dataSource = self;
}
(…)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//return [self.workout.stepsList count];
NSInteger counter = 0;
counter = [self.workout.stepsList count];
return counter;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Set up the cell...
// Pending
return cell;
}
- (void)stepDetailsViewControllerDidDone:(StepDetailsViewController *)controller
{
[self.workout.stepsList addObject:controller.step];
NSInteger counter = [self.workout.stepsList count];
[self.stepsListTableView beginUpdates];
NSArray *paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:(counter-1) inSection:0]];
[self.stepsListTableView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationTop];
[self.stepsListTableView endUpdates];
[self.stepsListTableView reloadData];
}
(…)
Also in the storyboard, I have setup the outlets delegate and dataSource to be the controller view.
Any idea ?
Regards,
JoanBa
I have solved the issue. I have discovered using debugger that method reloadData was called for a different UITableView than the one initialized in viewDidLoad.
I reviewed the UITableView settings in storyboard, which aparently were correct but I have deleted them and created again. Now it works.
In UIViewController header I have the line
#property (nonatomic, retain) IBOutlet UITableView *stepsListTableView;
and in implementation I have commented lines
//self.stepsListTableView.delegate = self;
//self.stepsListTableView.dataSource = self;
And, of course, in storyboard I have defined for the UITableView the following relationships:
Outlets: dataSource, delegate --> UIViewController
Referencing outlet: UITableView --> UIVIewController
That's it !!
You may have the table view set to static content. Select the UITableView in Storyboard and in the "Attributes Inspector" section of the menu on the right of screen select the "Content" field under the "Table View" header and set the value to "Dynamic Prototypes".
Screenshot for clarity:
This little trick caught me out several times when I was starting out.