How to add a UITableView in UIScrollView with PageControl - iphone

In my ViewController, i have two uitableview. One of them is static with one section and two rows, and the other has so one section but four rows.
I would like to do an UIScrollview with UIPageControl, and in each page, i would like to add the second tableView with four rows. But number of page in scrollView can be change. So i try with UILabel and it works but with tableView i can't see it.
I don't know if you understand my problem. I put the code of my loop.
for (int i = 1; i < [listAllContactDetails count] + 1; i++)
{
UILabel *nomContact = [[UILabel alloc] initWithFrame:CGRectMake((i-1)*320, 20, 320, 30)];
nomContact.backgroundColor = [UIColor yellowColor];
nomContact.text = [[listAllContactDetails objectAtIndex:i-1] valueForKey:#"name"];
[scroller addSubview:nomContact];
[nomContact release];
}
scroller.delegate = self;
scroller.contentSize = CGSizeMake(320*[listAllContactDetails count], 249);
scroller.pagingEnabled = YES;
pageControl.numberOfPages = [listAllContactDetails count];
pageControl.currentPage = 0;
This code right with UILabel but not with UITableView.
Thank for your help.

You should be able to add a UITableView to each page by setting its frame in a loop just the same as you are doing with the labels.
The problem is hooking up the data source. You'll need to bind each table view's datasource property to your view controller inside your loop. Then give each table in the loop a different .tag property based on the loop index. In your datasource methods you'll need to check the tag of the tableview to work out which page it is. Like this:
for (int i = 0; i < [listAllContactDetails count]; i++)
{
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(i*320, 0, 320, 200) style:...]
tableView.dataSource = self;
tableView.tag = i + 1;
[scroller addSubview:tableView];
[tableView release];
}
scroller.delegate = self;
scroller.contentSize = CGSizeMake(320*[listAllContactDetails count], 249);
scroller.pagingEnabled = YES;
pageControl.numberOfPages = [listAllContactDetails count];
pageControl.currentPage = 0;
...
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger tag = tableView.tag;
switch (tag)
{
case 1:
return numberOfRowsForPage1;
case 2:
return numberOfRowsForPage2;
etc...
}
}

Related

How to get UILabel Tags in iPhone

I am creating labels dynamically from NSMutableArray.While creating labels I have set tags for each label. I have one NSMutableArray named wordArray. Now, I want to check my string is available in wordArray or not,
I can check this using :
[wordArray containsObject:wordStr];
For creating labels dynamically :
UILabel *wordLabl;
int tagValue3 = 1;
for (int iloop = 0; iloop < [wordArray count]; iloop++)
{
wordLabl = [self addwordLabelRect:CGRectMake(80 * iloop + 20, 420 , 100, 20)andTag:tagValue3];//30 + 35 30 * iloop+
[self.view addSubview:wordLabl];
tagValue3 += 1;
}
-(UILabel *)addwordLabelRect:(CGRect)rect andTag:(int)integerValue
{
wordLabel = [[UILabel alloc] init];
wordLabel.frame = rect;
wordLabel.userInteractionEnabled = YES;
wordLabel.tag = integerValue;
wordLabel.backgroundColor = [UIColor clearColor];
wordLabel.font = [UIFont systemFontOfSize:15];
wordLabel.text = [NSString stringWithFormat:#"%#",[wordArray objectAtIndex:integerValue - 1]];
wordLabel.textAlignment = NSTextAlignmentCenter;
wordLabel.textColor = [UIColor whiteColor];
return wordLabel;
}
Using above code I am creating labels and Tags.
But,If wordArray contains the string I want to change the textColor of that label.I think this can be done using Tag , but how can I get the tag value of the label.
Sorry, I overlooked you code... You just need to add following lines where you want to access your appropriate label:
if([wordArray containsObject:wordStr])
{
UILabel *label = (UILabel *) [self.view viewWithTag:([wordArray indexOfObject:wordStr] - 1)];//since u started tag assignment from 1
label.textcolor = [UIColor yellowColor];
}
I guess you're doing something like that to set the tags ?
for (NSUInteger i = 0; i < [wordArray count]; ++i) {
UILabel * label;
// setup your label...
[label setTag:i];
[yourView addSubview:label];
}
If so, just do :
NSUInteger index = [wordArray indexOfObject:wordStr];
if (index != NSNotFound) {
UILabel * label = [yourView viewWithTag:index];
// do whatever you want with your label
}
Good luck.
if you want to get UILabel from its Tag.
you can use following loop
int i=0;
for (NSObject *view in self.View.subviews)
{
if ([view isKindOfClass:[UILabel class]])
{
label = (UILabel *)[[self view] viewWithTag:wordArray[i]];
NSLog(#"%#",label.text);
//here you get your label
}
i++;
}

how to hide SPUserResizableView with tag in iphone?

Hi I have a method in which I have created 3 views on a click;
-(void)method{
for (i=0; i<3; i++) {
NSLog(#"i is: %d",i);
NSLog(#"i is: %d",i);
userResizableView = [[SPUserResizableView alloc] initWithFrame:CGRectMake (100,41,60,60)];
userResizableView.tag = i;
imageVw1 = [[UIImageView alloc]initWithFrame:userResizableView.bounds];
imageVw1.image = [UIImage imageNamed:#"redacted2.jpg"];
imageVw1.userInteractionEnabled=YES;
imageVw1.autoresizesSubviews = YES;
imageVw1.alpha = 0.93; // for opacity
userResizableView.contentView = imageVw1;
userResizableView.delegate = self;
[userResizableView showEditingHandles];
currentlyEditingView = userResizableView;
lastEditedView = userResizableView;
[self.view addSubview: userResizableView];
[userResizableView release];
}
}
Now in another method I want to hide these views what I created last.
but I am not able to do it. I am hiding my view is like-
-(void)Hide_method{
// int type instance variable, and a=0; in viewdidload;
userResizableView.tag = a;
userResizableView.hidden = YES;
a++;
}
But only one view is hide and the rest are remaining, no matters how amny times I clicked Hide_method, only one view is hide.
My question is that how to hide last view what I created last. Means views hide like 3,2,1,0. On every time when I clicked hide_method.
Any Idea or suggestions would be highly welcome.
Provide unique tag to not clash with any other subview of self.view with same tag like say any number 999 +.
Make changes accordingly:
for (i=0; i<3; i++) {
SPUserResizableView *userResizableView = [[SPUserResizableView alloc] initWithFrame:CGRectMake(100,41,60,60)];
userResizableView.tag = 999+i;
..........
..........
}
Now hide method would be:
-(void)Hide_method{
// find SPUserResizableView with unique tag
for (i=0; i<3; i++) {
if(i == 2){
SPUserResizableView*userResizableView = (SPUserResizableView *)[self.view viewWithTag:999+i];
userResizableView.hidden = YES;
}
}
EDIT : To restore frame to original store while allocating view
CGRect prevFrame = userResizableView.frame //take frame while allocating userResizableView
Now restore whenever you wanted to original frame like this:
SPUserResizableView*userResizableView = (SPUserResizableView *)[self.view viewWithTag:999+2]; //get last userResizableView
userResizableView.frame = prevFrame;
-(void)Hide_method{
// int type instance variable, and a=0; in viewdidload;
//userResizableView.tag = a;
SPUserResizableView *_view = (SPUserResizableView *)[self.view viewWithTag:a];
userResizableView.hidden = YES;
a++;
}
Try this:
-(void)Hide_method{
//UIView *v = [self.view viewWithTag:a];
SPUserResizableView *v = (SPUserResizableView *)[self.view viewWithTag:a];
if(v)
[v removeFromSuperview];
a++;
}
Method to remove view having tag=a
for (UIView *subview in [self.view subviews])
{
if (subview.tag == a)
{
[subview removeFromSuperview];
}
}

Using dequeueReusableCellWithIdentifier with multiple tables in a UIScrollView

My app's UI requires grids of 9 images that are paginated.
To do this I'm creating several UITables inside a UIScrollView (with a UIPageControl):
My table is coming from a xib file that I register. Each table cell has 3 buttons in it.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
int numPages = ([self.images count] + 8) / 9;
NSLog(#"Page count: %i", numPages);
// Set pages
self.pageControl.numberOfPages = numPages;
for (int i = 0; i < numPages; 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];
subview.backgroundColor = [UIColor blackColor];
[self.scrollView addSubview:subview];
// Let's try and draw a table
UITableView *table = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
table.delegate = self;
table.dataSource = self;
table.tag = 100 + i;
table.separatorColor = [UIColor clearColor];
[table setBackgroundColor:[UIColor blackColor]];
[table registerNib:[UINib nibWithNibName:#"Cell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:#"TestCell"];
[self.scrollView addSubview:table];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * numPages, self.scrollView.frame.size.height);
}
My problem is that the view loads slowly (5+ seconds). The images being loaded into the buttons are local and not large.
My cellForRowAtIndexPath looks like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TestCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// What table/page are we looking at?
// Check the tag
int page = tableView.tag - 100;
NSLog(#"We are on page/table: %i", page);
// Configure the cell
// Our cells have 3 button images
// So we'll loop and set each image
for (int i = 0; i < 3; i++) {
// From our images array, the item we want
// Will be the row * 3 + i
int imageIndex = (page * 9) + (indexPath.row * 3) + i;
NSLog(#"Image index is: %i", imageIndex);
// Button tags are 10-12 (or 10 + i)
UIButton *button = (UIButton *)[cell viewWithTag:(10 + i)];
[button addTarget:self action:#selector(poseSelected:) forControlEvents:UIControlEventTouchUpInside];
// We have to make sure we don't go out of the bounds of
// our images array (might not have /3 exactly)
if (imageIndex > [self.images count] - 1) {
// We have an extra button, let's hide it
button.hidden = YES;
} else {
// Get the image and file nameefrom our array
NSDictionary *imageDict = (NSDictionary *)[self.images objectAtIndex:imageIndex];
NSString *fileName = [imageDict objectForKey:#"file"];
// Make the thumb name
NSString *thumbName = [fileName stringByReplacingOccurrencesOfString:#".jpg"
withString:#"-THUMB.jpg"];
// NSLog(#"Thumb name is: %#", thumbName);
// Set the button image
UIImage *btnImage = [UIImage imageNamed:thumbName];
[button setImage:btnImage forState:UIControlStateNormal];
}
}
return cell;
}
When I look at the log output, All of the cells are getting setup right away. Why is this?
Shouldn't table views off the screen not be rendered? Or am I using dequeueReusableCellWithIdentifier the wrong way?
You are correct, you are misinterpreting how cellForRowAtIndexPath and a scroll view works. Your cells are getting loaded immediately because your tableviews are all subviews in the scrollview. So the tableviews as subviews will get loaded immediately when the scrollview is added to the view hierarchy. The cellForRowAtIndexPath does recycling for cells that are disappearing when you scroll a tableview (by design this makes sense). My recommendation would be to use a recycling scrollview, or even better a custom gridview implementation (appears to be what you need). I have have some examples in my library, but it might be worth search for the best grid view or image viewer than meets your needs. My library can be found here:
https://github.com/daltoniam/GPLib-iOS
You are going to want to take a look at:
https://github.com/daltoniam/GPLib-iOS/tree/master/GPLib/Views/ImageViews
https://github.com/daltoniam/GPLib-iOS/tree/master/GPLib/Views/GridView
As I stated above, it might be worth looking for a grid view/image view more along the lines your UI needs then what my lib provides, but should be a good starting point. Any questions let me know.

loading multiple view controllers in a page scroll view and its interaction

I need to implement a page scroll view. Each page view should load a separate view controller. On performing actions/events on the view controller should update/refresh the page view.Is it possible? If yes , how? It would be very much grateful If you are able to give me pointers in this regard.
-(void)createScrollView
{
myScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 440)];
myScroll.pagingEnabled = YES;
myScroll.showsHorizontalScrollIndicator = YES;
myScroll.showsVerticalScrollIndicator = YES;
myScroll.scrollsToTop = NO;
myScroll.delegate = self;
int n = [array count];
myScroll.contentSize = CGSizeMake((320 * n), 440); // n = number of views
for (int i = 0 , X = 0; i < [photoArray count]; i++ , X += 320)
{
UIImageView *img =[[UIImageView alloc]initWithFrame:CGRectMake(X,0, 320, 480)]; //here view for you
CGRect frame;
frame.size.width=320; frame.size.height=240;
frame.origin.x=0; frame.origin.y=88;
[img addSubview:asyncImage];
img.contentMode=UIViewContentModeScaleAspectFit;
[myScroll addSubview:img];
}
}
- (void) viewDidLoad
{
[self createScrollView];
[myScroll scrollRectToVisible:CGRectMake(320*photoNumber, 0, 320 , 240) animated:NO];
[self.view addSubview:myScroll];
}
its a sample code .... try it with changes... hope that finally work for you...

Display A TTStyledTextLabel in a UITableView

How can I set a TTStyledTextLabel inside of a UITableView.
Each TTStyledTextLabel contains Some parsed HTML.
Heres what I have I realize its probably completely wrong.
TTStyledTextLabel* label = [[TTStyledTextLabel alloc] autorelease];
cell.textLabel.text = [TTStyledText textFromXHTML:tempString lineBreaks:YES URLs:YES];
App Crashes on launch. I think its because I am setting the .text property with something that is not text. However, I don't know what else to set.
The following code will do what you want. Unfortunately, however, I cannot figure out how to automatically set the height. If memory isn't an issue you could keep a seperate array of TTStyledTextLabels and reference their heights.
in your loadView:
CGRect cgRct2 = CGRectMake(0, 35, 320, 375); //define size and position of view
tblView = [[UITableView alloc] initWithFrame:cgRct2 style:UITableViewStylePlain];
tblView.dataSource = [self constructDataSource];
tblView.delegate = self;
//[tblView reloadData];
[myView addSubview:tblView];
in your class:
-(TTListDataSource *)constructDataSource {
NSLog(#"constructDataSource");
NSMutableArray * namesArray = [[NSMutableArray alloc] init];
//ADD ITEMS
[namesArray addObject:[TTStyledText textFromXHTML:[NSString stringWithString:#"some XHTML"]]];
TTListDataSource * dataSource = [[TTListDataSource alloc] init];
for (int i = 0; i < [namesArray count]; i++) {
TTStyledText * text = [namesArray objectAtIndex:i];
[dataSource.items addObject:[TTTableStyledTextItem itemWithText:text]];
}
[namesArray release];
return dataSource;
}