i am working in a project in which i am using UITableView to display data. The orientation of my project is Landscape. now i want to add portrait orientation mode to my project so that any user can use app in portrait as well as in landscape mode.
Is there any code or method available by the help of which i will change the orientation of UITableView from horizontal to vertical??
i want the orientation something like this
Just develop 2 different prototypes with unique identifiers, provide them in cellForRowAtIndexPath based on current orientation.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
LatestNewsCell *cell = (LatestNewsCell *)[tableView dequeueReusableCellWithIdentifier:#"portraitCell" forIndexPath:indexPath];
// configure cell
return cell;
} else {
LatestNewsCell *cell = (LatestNewsCell *)[tableView dequeueReusableCellWithIdentifier:#"landscapeCell" forIndexPath:indexPath];
// configure cell
return cell;
}
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[self.tableView reloadData];
}
In the case of the cell with more labels, you just create additional IBOutlets for these and reuse the existing ones for the labels that are used in both orientations.
I believe you just need to setup your project to support portrait orientation. After that, your UITableView will rotate as you rotate your device or simulator.
I have created a simple project to test this. Check the output here.
Notice that sometimes, the view correctly rotates, and sometimes not. Must be a bug.
Related
I have added an UITableView to Main.storyboard without setting any additional attributes. On the iPhone the table fills out in the portrait as well as in the landscape mode, the complete screen, which is the desired behavior. However, when starting the app on an iPad, the table looks like this:
When I add the following constraints to the table view:
Leading Space to Container Margin
Trailing Space to Container Margin
Vertical Spacing to Top Layout Guide
Vertical Spacing to Bottom Layout Guide
The issue is fixed on the iPad (portrait & landscape mode), but on the iPhone the first cell in the landscape mode is hidden:
How can I fix this? This issue does not occur in the portrait mode on the iPhone, nor does it happen on the iPad in both modes.
BTW, the controller attribute "Adjust Scroll View Insets" is enabled.
For the inset please add this below code
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if ([tableView respondsToSelector:#selector(setSeparatorInset:)]) {
[tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([tableView respondsToSelector:#selector(setLayoutMargins:)]) {
[tableView setLayoutMargins:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:#selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
For making the tableView fit in all devices so it fits in the whole screen add the below shown constraints.
The resulting TableView will be
I knew this question is old, so many answers was already posted here.
The problem is, the solutions are working fine with iPhone's. But when trying it with iPad, I still faces some issue.
Refer the below images(First one is portrait mode and the second one is in landscape mode).
I used the below code in my view controller where I wrote table view's delegate methods.
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if ([tableView respondsToSelector:#selector(setSeparatorInset:)]) {
[tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([tableView respondsToSelector:#selector(setLayoutMargins:)]) {
[tableView setLayoutMargins:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:#selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
Question:
Is there is any way to remove these left empty space of table view's separator in iPad? Should I include any extra check's beyond the iPhone's working code?
Confused!!
You may need to set the Table View's
cellLayoutMarginsFollowReadableWidth to NO if you want to customize
insets or margins. Your mileage may vary, this is not documented very
well.
This property only exists in iOS 9 so be sure to check before setting.
if([myTableView respondsToSelector:#selector(setCellLayoutMarginsFollowReadableWidth:)])
{
myTableView.cellLayoutMarginsFollowReadableWidth = NO;
}
Using iOS 8.3 simulator and Xcode 6.3.2.
I'm using the self sizing cells technique in iOS 8, and it works terrifically when the cell has no accessory view, but breaks when the cell has an accessory view. I set constraints on the label inside the cell:
Then I use an estimatedRowHeight and set the rowHeight to UITableViewAutomaticDimension (I'm omitting tableView:numberOfRowsInSection: here, but it just returns 3 for this example):
#implementation MyTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.estimatedRowHeight = 44.0f;
self.tableView.rowHeight = UITableViewAutomaticDimension;
}
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:#"myCell" forIndexPath:indexPath];
cell.titleLabel.text = #"Test comment Test comment Test comment Test comment Test comment Test comment Test comment Test comment Test comment Test comment Test comment ";
return cell;
}
#end
And bam, everything looks great:
But when I add an accessory view in the Storyboard, without changing any of the code:
the first cell goes to hell. Xcode's view debugger tells me the label height is 1785 points, and I had to use a gif to show it here:
I do notice that the other two cells are sized fine. I also note that when I rotate the simulator, the first cell gets resized properly, including after it's rotated back to portrait.
Does anyone know why the accessory view messes this up so badly, and what I can do to fix it? A sample project is available at http://github.com/UberJason/SelfSizingNightmare.
This is a bug indeed. I have sent your project to Apple. There is a reply from Apple.
It seems to be caused by the label inside the cell having numberOfLines = 0.
Although I'm not sure why, it looks as though adding the accessory to the cell leaves auto layout unable to calculate the height of the label when the table first loads.
I was able to fix your example by adding a preferred max width to the label - which seems to be enough to let the layout system figure out the height in time:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:#"myCell" forIndexPath:indexPath];
// Give the layout system something to help it estimate the label height
cell.titleLabel.preferredMaxLayoutWidth = self.titleLabel.bounds.size.width;
cell.titleLabel.text = #"Test comment Test comment Test comment Test comment Test comment Test comment Test comment Test comment Test comment Test comment Test comment ";
return cell;
}
Alternatively, calling [tableView reloadData] in viewDidAppear also seems to fix it.
I have an iOS app with several UITableViews in them all of which worked as intended
. I upgrade the app to handle iOS8
since then I had a problem with loading a custom cell into the table view who's nib had the box in the ib checked 'use auto layout'. I then uncheck all of these on my custom cell and since then the cells of all my UITableViews not only don't call didSelectRowAtIndex path method but are not highlighted on touch.
I have check that all the cells are active by adding
if(cell.userInteractionEnabled){NSLog(#"is enabled");}else{NSLog(#"is not enabled");}
all of the loaded cells write 'is enabled' to the log
I am setting the delegate and data source via the ib in the storyboard and all of this was working prior to me changing the 'use auto layout' and upgrade to run on iOS 8.
what have i missed?
here is my code to create the cells
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TableCellWithNumberCellIdentifier";
if( events.count>indexPath.row &&[[[events objectAtIndex:indexPath.row] objectForKey:#"tag"] integerValue] == -1)
{
EventsMonthSeparator *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell.translatesAutoresizingMaskIntoConstraints=NO;
cell = (EventsMonthSeparator *)[EventsMonthSeparator cellFromNibNamed:#"EventsMonthSeparator"];
cell.date.text=[[events objectAtIndex:indexPath.row] objectForKey:#"date"];
[Functions setFontFamily:#"Neutra Display" forView:cell andSubView:YES];
if(cell.userInteractionEnabled){NSLog(#"is enabled");}else{NSLog(#"is not enabled");}
}
return cell;
}else
{
eventsRow *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell.translatesAutoresizingMaskIntoConstraints=NO;
cell = (eventsRow *)[eventsRow cellFromNibNamed:#"eventsRow"];
cell.description.text=[[events objectAtIndex:indexPath.row] objectForKey:#"name"];
cell.timedate.text=[[events objectAtIndex:indexPath.row]objectForKey:#"date"];
cell.image.image=[UIImage imageNamed:#"tiledrinks"];
cell.status.text=[Functions statusForIndex:[[[events objectAtIndex:indexPath.row] objectForKey:#"booked"] intValue]];
[Functions setFontFamily:#"Neutra Display" forView:cell andSubView:YES];
cell.tag=1;
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
if(cell.userInteractionEnabled){NSLog(#"is enabled");}else{NSLog(#"is not enabled");}
}
return cell;
}
}
Please Do check that Auto-Layout is OFF (Non selected tickMark) in your Custom cell's xib Also.
And in Your TableView check this default setting,
As attached in below image
I have same issue once, where I have give NO Selection in selection property of TableView's attribute inspector.
If you have done same then give Single selection there.
Hope this will help you !
If you have selected auto layout, cell.translatesAutoresizingMaskIntoConstraints=NO; will negate the auto layout that Xcode provides.
Also, could you check if all your tableViews' delegates are intact? If your delegate isn't set, the tapping on table view cell will not invoke the 'didSelectRow' method.
You can select "in single selection During Editing" in TableView,
for example:
I gave a look on QuizUp and I would like to make a similar menu on my iphone application.
I don't understand how they made the menu. It looks like a table view controller, but a new menu appears under the cell when the user tap on it.
Can someone give me some clues. Is it possible to do that with the native iOS SDK ? How ?
According to the developer of Quiz Up, the app was made using Cocos2D.
That being said, it is also possible to do it using a UITableView. Theres just a couple things you would need to do. The first is get tableView:heightForRowAtIndexPath: to return a different height for the row(s) you want to be expanded. For example, if you only want one row expanded you could use the following.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[tableView indexPathForSelectedRow] isEqual:indexPath]) {
return 100.0f;
}
return 44.0f;
}
Then what you need to do is call beginUpdates and endUpdates on your tableview whenever you want the height to update.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView beginUpdates];
[tableView endUpdates];
}
After that, you just need to layout your cell so that when it's expanded, it shows the additional view and when its collapsed it should hide that view.