UIScrollView scrolls Horizontally and UITableView scrolls vertically inside that but unable to load different data when scroll horizontally.
have one scrollview which scrolls horizontally on screen and inside of that i have added multiple tableview and want to display different different datas on tableview when swipe. i have tried this but with no luck :(
NSArray *arr1 = [[NSArray alloc] initWithObjects:#"1",#"2",#"3", nil];
NSArray *arr2 = [[NSArray alloc] initWithObjects:#"4",#"5",#"6", nil];
NSArray *arr3 = [[NSArray alloc] initWithObjects:#"7",#"8",#"9", nil];
NSArray *arr4 = [[NSArray alloc] initWithObjects:#"10",#"11",#"12", nil];
arrData = [[NSMutableArray alloc] initWithObjects:arr1,arr2,arr3,arr4, nil];
int width=0;
for (int i = 0; i<[arrData count]; i++) {
tblData = [[UITableView alloc] initWithFrame:CGRectMake(width, 20, 300, 245) style:UITableViewStylePlain];
tblData.dataSource = self;
tblData.delegate = self;
[tblData setBackgroundColor:[UIColor clearColor]];
tblData.separatorColor = [UIColor blackColor];
[scrHorizontal addSubview:tblData];
width+=300;
}
[self.scrHorizontal setContentSize:CGSizeMake(([arrData count])*300, self.scrHorizontal.frame.size.height)];
here 4 pages inside scrollview which includes 4 tableviews i want to display 1,2,3 on first table and 4,5,6 on second table when swipe scrollview and so on...
please help me thanks in advance.
I have seen your code , you can do with tag,
set tag to tableview in loop
for (int i = 0; i<[arrData count]; i++) {
tblData = [[UITableView alloc] initWithFrame:CGRectMake(width, 20, 300, 245) style:UITableViewStylePlain];
tblData.tag=i;
tblData.dataSource = self;
tblData.delegate = self;
[tblData setBackgroundColor:[UIColor clearColor]];
tblData.separatorColor = [UIColor blackColor];
[scrHorizontal addSubview:tblData];
width+=300;
}
now in cellForRowAtIndexPath method just use it
like
NSArray *arr = [arrData objectAtIndex:tableView.tag];
cell.textLabel.text = [arr objectAtIndex:indexPath.row];
check it ScrollViewDemo
Hope it Helps.
Related
I am following this example
I have added a button in the view
On selection the button image change to Green icon image.
How could I retain it using this example?
I had updated the existing code of your example to your requirement please find the below url for download the code and review it.
Link : https://www.dropbox.com/s/6xsr4o88khyijun/HorizontalTables.zip?dl=0
Highlight of the code which I had done it.
1) Take the NSMutableArray *arrSelection property in HorizontalTablesAppDelegate class.
2) Fill the data of arrSelection in ArticleListViewController_iPhone class of viewDidLoad method.
for (NSInteger i = 0; i < [self.articleDictionary.allKeys count]; i++)
{
HorizontalTableCell_iPhone *cell = [[HorizontalTableCell_iPhone alloc] initWithFrame:CGRectMake(0, 0, 320, 416) tag:i];
categoryName = [sortedCategories objectAtIndex:i];
currentCategory = [self.articleDictionary objectForKey:categoryName];
cell.articles = [NSArray arrayWithArray:currentCategory];
if(i == 0)
appDelegate.arrSelection = [[NSMutableArray alloc] init];
NSMutableArray *arrSubData = [[NSMutableArray alloc] init];
for(NSInteger j=0; j<currentCategory.count; j++)
[arrSubData addObject:[NSNumber numberWithBool:NO]];
[appDelegate.arrSelection addObject:arrSubData];
[arrSubData release];
[self.reusableCells addObject:cell];
[cell release];
}
3) On HorizontalTableCell_iPhone class of cellForRowAtIndexPath method
NSMutableArray *arrSelectionInfo = appDelegate.arrSelection[tableView.tag];
BOOL isSelect = [arrSelectionInfo[indexPath.row] boolValue];
if(isSelect)
[cell.btnSelection setBackgroundColor:[UIColor greenColor]];
else
[cell.btnSelection setBackgroundColor:[UIColor whiteColor]];
And didSelectRowAtIndexPath method
NSMutableArray *arrUpdate = appDelegate.arrSelection[tableView.tag];
[arrUpdate replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:YES]];
[tableView reloadData];
Hope this will work for you.
To keep button state selected you need to store information of selection on a dictionary for the cell. So once cell is reloaded, you can apply selection setting based on information you have.
There is no direct way (even if there was you should now use that, as it will lead to memory abuse) to keep it selected all the time.
I have 4 pickerview in a view controller and, I need that after 2 of them have been selected with the desired user preference I want the third to retrieve data from Parse based on the user's selection of the other two, any way I can implement this? Now I've each picker view with a tag but I can't implement a PFQuery inside the pickerview methods.
Thank you.
This is what I have, trying to do a custom pickerview but I would like to use the UIPickerView class methods.
#pragma mark - Select Schedule
-(void)selectSchedule:(NSString *)horari :(NSString *)origen :(NSString *)direccio{
PFQuery *queryParada = [PFQuery queryWithClassName:#"EixBusParades"];
[queryParada whereKey:#"Parada" equalTo:origen];
PFQuery *queryHours = [PFQuery queryWithClassName:#"EixBusParades"];
[queryHours whereKey:#"hSetmana" equalTo:horari];
PFQuery *queryDireccio = [PFQuery queryWithClassName:#"EixBusParades"];
[queryDireccio whereKey:#"direccio" equalTo:direccio];
PFQuery *querySchedule = [PFQuery orQueryWithSubqueries:#[queryHours,queryParada,queryDireccio]];
NSArray *objects = [querySchedule findObjects];
NSArray *depHours = [[objects objectAtIndex:0]objectForKey:#"Horaris"];
// Copy each object from the array depHours to the pickerview array in order to show it
/*int i;
int j = 0;
for (i = 0; i < [depHours count]; i++) {
[self.hores objectAtIndex:j] = [depHours objectAtIndex:i];
j++;
}*/
}
- (IBAction)tfButton:(id)sender {
[self.tfHora becomeFirstResponder];
// Retrieve correct hour data from Parse
[self selectSchedule:self.tf.text :self.tfOrigen.text :self.tfDireccio.text];
}
- (void)cancelTouched:(UIBarButtonItem *)sender{
// hide the picker view
[self.tfHora resignFirstResponder];
}
- (void)doneTouched:(UIBarButtonItem *)sender{
// hide the picker view
[self.tfHora resignFirstResponder];
}
Initialitzation of the picker view in viewdidload.
self.tfHora = [[UITextField alloc] initWithFrame:CGRectZero];
[self.view addSubview:self.tfHora];
UIPickerView *pickerHora = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
pickerHora.showsSelectionIndicator = YES;
pickerHora.dataSource = self; // Server
pickerHora.delegate = self;
self.tfHora.inputView = pickerHora;
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
toolBar.barStyle = UIBarStyleBlackOpaque;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneTouched:)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelTouched:)];
// the middle button is to make the Done button align to right
[toolBar setItems:[NSArray arrayWithObjects:cancelButton, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], doneButton, nil]];
self.tfHora.inputAccessoryView = toolBar;
I have this piece of code, which is in init.
This should be some swipe cell, which is created by UIScrollView and there is some another view (my own action view) below cell.
My problem: if I want swipe my cell, UIScrollView doesnt call delegate methods :-/
Any suggestions? thx
self.cellState = kCellStateClosed;
self.cellScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kSWIPECELL_WIDTH, kSWIPECELL_HEIGHT)];
self.cellScrollView.delegate = self;
self.cellScrollView.showsHorizontalScrollIndicator = NO;
self.cellScrollView.scrollsToTop = NO;
self.cellScrollView.scrollEnabled = YES;
self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(selectCell)];
self.longPressGestureRecognizer = [[PAPLongPressGestureRecognizer alloc] initWithTarget:self action:#selector(scrollViewPressed:)];
self.longPressGestureRecognizer.minimumPressDuration = 0.1;
[self.cellScrollView addGestureRecognizer:self.tapGestureRecognizer];
[self.cellScrollView addGestureRecognizer:self.longPressGestureRecognizer];
// Create the content view that will live in our scroll view
self.scrollViewContentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kSWIPECELL_WIDTH, kSWIPECELL_HEIGHT)];
self.scrollViewContentView.backgroundColor = [UIColor whiteColor];
[self.cellScrollView addSubview:self.scrollViewContentView];
// Add the cell scroll view to the cell
UIView *contentViewParent = [self.subviews firstObject];
NSArray *cellSubviews = [contentViewParent subviews];
[self insertSubview:self.cellScrollView atIndex:0];
for (UIView *subview in cellSubviews)
{
[self.scrollViewContentView addSubview:subview];
}
self.containingTableView.directionalLockEnabled = YES;
//alloc action view
self.actionView = [[PAPActionCellView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height) andPages:[self createActionPages] parentCell:self];
[self.cellScrollView insertSubview:self.actionView belowSubview:self.scrollViewContentView];
What you are missing is to set the contentSize of your UIScrollView, when you set it to some bigger value than its bounds, it will start to scroll, i.e:
self.cellScrollView.contentSize = CGSizeMake(2* kSWIPECELL_WIDTH, kSWIPECELL_HEIGHT);
I'm hoping somebody can help me out here, I'm not sure what else to do. I have a UIScrollView that loads a set of 44 images, and then allows the user to page through them. My code was working, then suddenly it stopped. I have no idea what I have changed so any help would be much appreciated:
The previous view is a UITableView, it passes a string along with the name of the cell that was selected. Here is the ViewDidLoad Method from the ScrollView:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Answer"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(answerPressed:)];
self.navigationItem.rightBarButtonItem = rightButton;
if([questionSetName isEqualToString:#"Limitations"]){
[self limitationsView];
}
}
Here is the limitationsView:
-(void)limitationsView{
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, -44, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSArray *unsortedArray = [[NSBundle mainBundle] pathsForResourcesOfType:#"png" inDirectory:#"Images/Limitations"];
NSMutableArray *limitationImages = [[NSMutableArray alloc] init];
for (int x = 0; x<[unsortedArray count]; x++) {
NSString *fileName = [[unsortedArray objectAtIndex:x] lastPathComponent];
[limitationImages insertObject:fileName atIndex:x];
}
NSArray *sortedArray = [limitationImages sortedArrayUsingFunction:finderSortWithLocal
context:(__bridge void *)([NSLocale currentLocale])];
for(int i = 0; i< [sortedArray count]; i++){
CGFloat xOrigin = i * self.view.bounds.size.width;
UIImageView *limitationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];;
[limitationImageView setImage:[UIImage imageNamed:[sortedArray objectAtIndex:i]]];
limitationImageView.contentMode = UIViewContentModeScaleAspectFit;
[scroll addSubview:limitationImageView];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * [sortedArray count], self.view.frame.size.height);
[self.view addSubview:scroll];
}
When I get to the end of the LimitationsView method the sortedArray contains all 44 images, and the proper names, if I try and set the background color of the scrollview I can successfully do that. but my images are not loading and I am at a loss as to why?
I would suggest using
NSLog(#"image:%#",[UIImage imageNamed:[limitationsArray objectAtIndex:i]]);
to see if the image is null at that point. If that's the case, then there is a problem with the strings you are storing in that array i.e., they are not identical to the actual names of your images
I'm trying to recreate a view controller similar to the "weather" app that comes stock on the iPhone. I've got a scrollview that contains an individual tableview per page, and I'm struggling to figure out a way to specify the data for each table view.
The tricky part is that the info in each tableview and the tableviews themselves change based on user defaults. Basically, I have an Array of dictionaries stored in user defaults, and each object in this array has its own tableview. Each dictionary contains a title, a lat and a long. When I create the tables, I also use the lat and long to get some data from the internet via an api call and parser. Here's the code:
- (void)setupScrollView
{
scrollView.delegate = self;
[self.scrollView setBackgroundColor:[UIColor clearColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
scrollView.showsHorizontalScrollIndicator = NO;
//this is an array of dictionaries that hold a location title, as well as a lat and lng.
NSArray *arrayForLocations = [NSArray arrayWithArray:[appDelegate.defaults objectForKey:#"arrayOfLocationDicts"]];
NSLog(#"Array of Location Dicts holds: %#",arrayForLocations);
for (int i = 0; i < arrayForLocations.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
[self.scrollView addSubview:subview];
UITableView *tbView = [[UITableView alloc]initWithFrame:CGRectMake(0, 40, scrollView.frame.size.width, scrollView.frame.size.height - 40)];
tbView.backgroundColor = [UIColor grayColor];
tbView.tag = i;
tbView.delegate = self;
tbView.dataSource = self;
[subview addSubview:tbView];
UILabel *locationLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 260, 40)];
locationLabel.textColor = [UIColor blackColor];
locationLabel.text = [[arrayForLocations objectAtIndex:i]objectForKey:#"location_address"];
[subview addSubview:locationLabel];
pageControl.numberOfPages = [arrayForLocations count];
//get coordinate from dictionary
CLLocationCoordinate2D eventCoordinate;
eventCoordinate.latitude = [[[arrayForLocations objectAtIndex:i]objectForKey:#"location_latitude"]floatValue];
eventCoordinate.longitude = [[[arrayForLocations objectAtIndex:i]objectForKey:#"location_longitude"]floatValue];
//turn coordinate into data
SDJConnection *connection = [[SDJConnection alloc]init];
NSMutableArray *singleDataSource = [connection getEventInfoWithCoordinate:eventCoordinate];
//store data in array
[arrayOfDataSources addObject:singleDataSource];
NSLog(#"array of Data Sources in the scrollsetup: %#",arrayOfDataSources);
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * arrayForLocations.count, self.scrollView.frame.size.height);
}
So now my problem is telling each of these tables what data to display. My first thought was to set the tag of the table, as I do above, and then have something like this in the cellForRowAtIndexPath -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
int i = tableView.tag;
if (tableView.tag = i) {
cell.textLabel.text = [[[arrayOfDataSources objectAtIndex:i]objectAtIndex:indexPath.row]eventTitle];
}
that unfortunately hasn't been working for me correctly. Does anyone have any thoughts as to how to get this done?
Thanks!!
For sure
if (tableView.tag = i) {
should be
if (tableView.tag == i) {
and even then, it's an odd construct -- not sure what you're trying to achieve there. You set the i to the tag and then you check to see if the tag is i? Doesn't really make sense -- that's where I'd suggest you take a close look and rethink your logic.