I have a problem with my code
scroll=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
scroll.backgroundColor=[UIColor clearColor];
scroll.pagingEnabled=YES;
scroll.contentSize = CGSizeMake(320*5, 460);
CGFloat x=0;
for(int i=1;i<6;i++)
{
UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake(x+10, 10, 300, 440) style:UITableViewStyleGrouped];
table.delegate=self;
table.dataSource=self;
[scroll addSubview:table];
[table release];
x+=320;
}
[self.view addSubview:scroll];
scroll.showsHorizontalScrollIndicator=NO;
[scroll release];
This is the code for the pagination through scrollview with tableview,but i need to put data in these 5 tableview cells that i create above.How to put data through these tableviews.(Data from xml parsing)
Thanks advance
You've correctly set self as the tableView's data source and delegate.
The table views will now ask for the data and you need to implement the UITableViewDataSource protocol.
Each of those methods will get a reference to the table view that's asking for information, so you can use that to figure out which data to provide (for which of your 5 table views).
You may want to have a look at the Table View Programming Guide.
Related
i.e. There is an object with specific data above a TableView, and when the TableView is scrolled up, I want the object above to be pushed off screen. Similar to the Maps app when you're looking at a business page, where there's the header, and then a TableView below with the phone, homepage, etc below.
Thanks!
There are many ways to achieve this.
1- Make the object with specific data above the tableview TableView
Header. From the question it look like you need help for that also so here how you will do it.
-(void)viewDidLoad
{
// ...
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
[headerView addSubview:imageView];
UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
[headerView addSubview:labelView];
self.tableView.tableHeaderView = headerView;
[imageView release];
[labelView release];
[headerView release];
// ...
}
2- Implement scrollview delegate inside check if the scrollview is tableview then hide you object (view).
Background: I have a UIViewController which has a UITableView added programmatically. At the top of the UITableView I have a tablHeaderView in which I have placed a UIView. This UIView has a UISearchBar and a segmentedControl. The idea being: that a user can sort the UITableView by some basic categories such as 'Date/Time' or 'Location' etc. They can also search by an item in the programme.
Problem: When I tap the search bar it resizes (which I don't want) and then when I cancel the search it resizes again and stays there until the UIViewController is exited and loaded again.
Code:
-(void)loadTableView
{
usableSpace = [[UIScreen mainScreen] applicationFrame];
usableWidth = usableSpace.size.width;
usableHeight = usableSpace.size.height;
_tableView = [[UITableView alloc] init];
[_tableView setFrame:CGRectMake(0,0,usableWidth, usableHeight)];
[_tableView setDataSource:self];
[_tableView setDelegate:self];
[self.view addSubview:_tableView];
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, usableWidth, 44)];
_searchBar.showsCancelButton = YES;
NSLog(#"searchBar height = %fl", _searchBar.frame.size.height);
segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Date/Time", #"Location", #"Speaker", nil]];
segmentedControl.frame = CGRectMake(0, (_searchBar.frame.size.height), usableWidth, (usableHeight * 0.075));
[segmentedControl addTarget:self action:#selector(sortList) forControlEvents:UIControlEventValueChanged];
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, usableWidth, (usableHeight * 0.15))];
[headerView addSubview:_searchBar];
[headerView addSubview:segmentedControl];
_tableView.tableHeaderView = headerView;
[self.view addSubview:_tableView];
self.tableView = _tableView;
}
What I have tried: I have tried setting the size of the SearchBar, not setting its size. Not having it in a UIView in the tableHeaderView, instead just having it there on its own.
Why does it resize at all and how can I get it to stop?
EDIT: I have just tried the following: In storyboard (where the UIViewController was originally created) I have selected the UIVC in question and in attributes inspector I deselected 'Under Top Bars' and 'Under Bottom Bars' and this appears to have fixed the first part of the animation problem. Now, when I tap in the search bar, the search becomes active but the searchBar does NOT resize. However, when I cancel the searchBar the searchBar still animates and resizes as per the last image in my screenshot. What could be causing THAT resizing?
i've been banging my head on this for too freakin long... props to this dude here for the clues.
if you want your UISearchBar to be in the header along with other views and to scroll with the header instead of sticking at the top, then you've got to remove it and re-add it after the search completes. to be clear, build your header like you already do and then throw this in there as well to handle the screwy animations.
-(void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
[self.mySearchBar removeFromSuperview];
self.mySearchBar.frame = CGRectMake(0, 0, 320, 44);
[self.table.tableHeaderView addSubview:self.mySearchBar];
}
As Timothy Moose notes here, "It seems that UISearchDisplayController makes an undocumented assumption that the search bar is the only content of the header view"
...yay apple.
Ok, after hours of reading up and trying different things I think I have found out how to have a UISearchBar where I want it and it seems to work ok. I believe the problem was to do with either having multiple views in the tableHeaderView OR the UISearchBar did not like being with another view inside a 'containing' view.
Here is the code I ended up with that allowed me to have a segmentedControl at the top of my UITableView AND a UISearchBar which actually sat at the top of my UIView (to which the UITableView was added).
-(void)loadTableView
{
usableSpace = [[UIScreen mainScreen] applicationFrame];
usableWidth = usableSpace.size.width;
usableHeight = usableSpace.size.height;
_tableView = [[UITableView alloc] init];
[_tableView setFrame:CGRectMake(0,0,usableWidth, usableHeight)];
[_tableView setDataSource:self];
[_tableView setDelegate:self];
[self.view addSubview:_tableView];
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, usableWidth, 44)];
[_searchBar setShowsCancelButton:YES];
NSLog(#"searchBar height = %fl", _searchBar.frame.size.height);
segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Title", #"Date/Time", #"Speaker", nil]];
segmentedControl.frame = CGRectMake(0, (_searchBar.frame.size.height), usableWidth, (usableHeight * 0.075));
segmentedControl.selectedSegmentIndex = 0;
[segmentedControl addTarget:self action:#selector(sortList) forControlEvents:UIControlEventValueChanged];
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, usableWidth, (usableHeight * 0.15))];
[headerView addSubview:segmentedControl];
_tableView.tableHeaderView = headerView;
[self.view addSubview:_tableView];
[self.view addSubview:_searchBar];
self.tableView = _tableView;
}
This meant that I had:
UIViewController with a UINavigationController, UISearchBar, UITableView. The UITableView had a segmentedConrol. The UISearchBar sits just under the UINavigationController and is always present. The UISearchBar sits at the top of the UITableViewController and scrolls with the UITableViewCells.
If anyone knows why the UISearchBar acted as it did in the question then I would be grateful if they could leave a comment. I know that this problem (or very similar) has been experienced by MANY people and I haven't found a definitive answer anywhere.
I'm interested in implementing a paging UIScrollView experience, very similar to the current Twitter app, where there is a main paging UIScrollView (horizontal scrolling) at the top of the view hierarchy, and several other (vertical scrolling) UIScrollViews or UITableViews as the paging UIScrollView's subview.
I was taking reference from this WWDC video where they described how we could add a/several "zooming" scrollViews as a subview of a bigger "paging" scrollView.
In my current setup, I have:
A ViewController in storyboard with hierarchy as follows: View > ScrollView (contentScrollView) > Several subviews (UIImageViews, UIButtons and UILabels).
When that view is being loaded, I call a method setupPagingScrollView method in its viewDidAppear.
- (void)setupPagingScrollView
{
// Setup the PAGING UIScrollView
self.pagingScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 340, self.view.frame.size.height)];
self.pagingScrollView.contentSize = CGSizeMake(680, self.view.frame.size.height);
self.pagingScrollView.pagingEnabled = YES;
self.pagingScrollView.backgroundColor = [UIColor blackColor];
// Remove the CONTENT UIScrollView (from storyboard) from the view.
// Add the CONTENT UIScrollView (from storyboard), as a subview of the newly created PAGING UIScrollView
[self.contentScrollView removeFromSuperview];
[self.pagingScrollView addSubview:self.contentScrollView];
[self.view addSubview:self.pagingScrollView];
// Create the 2nd page, which is a UITableView
self.tableView = ({
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(340, 0, 320, self.view.frame.size.height) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
tableView.backgroundColor = [UIColor whiteColor];
tableView.tableFooterView = [UIView new];
tableView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0);
tableView;
});
[self.pagingScrollView addSubview:self.tableView];
// UINavigationBar for the UITableView in the 2nd page.
self.likesCommentsNavBar = ({
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(340, 0, 320, 44)];
UINavigationItem *navItem = [[UINavigationItem alloc] init];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:#[#"Likes", #"Comments"]];
[segmentedControl addTarget:self action:#selector(toggleLikesCommentsWithSegmentedControl:) forControlEvents:UIControlEventValueChanged];
segmentedControl.selectedSegmentIndex = 0;
navItem.titleView = segmentedControl;
navBar.items = #[navItem];
navBar;
});
[self.pagingScrollView addSubview:self.likesCommentsNavBar];\
}
My intended end result would be something like the twitter application, just that instead of panning between 3 UITableViews, my first view would be a custom view with some images, labels, and buttons, and the 2nd and 3rd page would be UITableViews.
I have created a mockup of my intended result
Currently, I am able to get the result I want but I realize that there is some choppiness in the performance. Am I doing something wrong here by allocating memory to so many views or something?
Many thanks!
Incase the image loading on the main thread is the problem here, here is an easy way to load images on a background thread
UIImageView *imageView = [[UIImageView alloc] init]; // can be your IBOutlet or something instead
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
UIImage *image = [UIImage imageNamed:#"imageName"];
dispatch_sync(dispatch_get_main_queue(), ^{
imageView.image = image; //can add some fade in animation or something here too
});
});
I am trying to place a UIImageView behind a the tableView in a view controller that is a subclass of UITableViewController. I have gotten the following code to sort of work, but the image scrolls with the rows in the table.
UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[backgroundView setImage:[UIImage imageNamed:#"woodbackground1"]];
[self.view addSubview:backgroundView];
[self.view sendSubviewToBack:backgroundView];
[self.tableView setBackgroundColor:[UIColor clearColor]];
I tried the suggestions in this post
Add UIView behind UITableView in UITableViewController code but they produce the same result, am image that across with the table.
Is there a way to place the image view behind the tableview so that the image does not move with the table?
Thanks,
Dan
Why not use
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background"]];
I am trying to display data in a UITableView, and the first row will be a label to identify the type of data. How can I keep the first row as a label when scrolling down?
Thanks!
You can do that with the TableView header. Here is an example:
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 60)];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 300, 40)];
headerLabel.text = NSLocalizedString(#"blablabla", #"");
headerLabel.backgroundColor = [UIColor clearColor];
[header addSubview:headerLabel];
myTable.tableHeaderView = header;
Use the header's of table views to do this specifically. All the functionality is already built into the UITableView class. In the UITableViewDataSource, just implement
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
I think one way you can implement it is by using the tableHeaderView property of the UITableView. Make the tableHeaderView look like the first row of your table and begin the table from the 2nd row.