Showing images on table cell click - ios

I am creating an iPhone app. In that I am displaying some content in a table cell. Now, I want to display the full-size image when click on the table cell.
I used the following code. But I don't know how to display the image in full screen when I select the table cell.
And also I want to get back when I tap on background when the image is in full screen.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"CustomCell"];
const char *dbpath = [_databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &_beaconDB) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:
#"SELECT vendor_name, enter_message, enter_image, received_date, time_interval FROM beacons WHERE id=%#", [unique objectAtIndex:indexPath.row]];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(_beaconDB,
query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_ROW)
{
title = [[NSString alloc]
initWithUTF8String:
(const char *) sqlite3_column_text(
statement, 0)];
description = [[NSString alloc]
initWithUTF8String:(const char *)
sqlite3_column_text(statement, 1)];
fullImg = [[NSString alloc]
initWithUTF8String:(const char *)
sqlite3_column_text(statement, 2)];
NSString *receivedDate = [[NSString alloc]
initWithUTF8String:(const char *)
sqlite3_column_text(statement, 3)];
NSString *timeInterval = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 4)];
BeaconAdTime *beaconAdTime = [[BeaconAdTime alloc] init];
NSDate *updatedTime = [beaconAdTime updatedDateTime:receivedDate andInterval:[timeInterval intValue]];
while (updatedTime!=nil)
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"MMM-dd hh:mm a"];
BeaconAdTime *beaconAdTime = [[BeaconAdTime alloc] init];
NSString *presentDateTime = [beaconAdTime presentDateTime];
NSDate *presentDateConvert = [dateFormatter dateFromString:presentDateTime];
if(updatedTime <= presentDateConvert)
{
// [message insertObject:description atIndex:indexPath.row];
// [vendor insertObject:title atIndex:indexPath.row];
cell.title.text = title;
cell.description.text = description;
cell.receivedDate.text = receivedDate;
break;
}
}
}
sqlite3_finalize(statement);
}
sqlite3_close(_beaconDB);
}
cell.title.text = vendor[indexPath.row];
cell.vendorImage.image = [UIImage imageNamed:#"gg.jpg"];
// cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:thumbnailImg]]];
return cell;
}

something like that:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = tableView.visibleCells[indexPath.row];
UIViewController *imageVC = [UIViewController new];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
imageVC.view.frame = CGRectMake(0, 0, screenWidth, screenHeight);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageVC.view.frame];
imageView.image = cell.imageView.image;
imageView.userInteractionEnabled=YES;
UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTouched:)];
[imageView addGestureRecognizer:tapGR];
[self presentViewController:imageVC animated:YES completion:nil];
}
let me know if you need explanation and/or further help

I think what you're looking for is a drill-down table interface.
Here's an example from Ray Wenderlich: http://www.raywenderlich.com/913/sqlite-tutorial-for-ios-making-our-app. Also, here's the official reference from Apple: https://developer.apple.com/library/ios/documentation/userexperience/conceptual/tableview_iphone/TableViewAndDataModel/TableViewAndDataModel.html.
Good luck!

Related

How to display multiple rows and columns from database into tableView?

I'm trying to display rows and columns from database into tableview. My approach is: First I will get data from database and then I will assign SQL statements into variables like this:
- (NSMutableDictionary *)arrayOfInquiries
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *querySQL = #"select * from inquiry_tb";
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(database,
query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
Inquiry *voucher = [[Inquiry alloc]init];
NSNumber *id = [[NSNumber alloc] initWithInt:sqlite3_column_int(statement, 1)];
NSString *name = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 2)];
NSString *date = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 3)];
NSString *branch = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 4)];
NSMutableDictionary *inquiries = [NSMutableDictionary dictionaryWithObjectAndKeys:id,#"id",name,#"name",date,#"date",branch,#"branch",nil];
sqlite3_reset(statement);
return inquiries;
}
}
}
return nil;
}
And Im planning to return in to main view controller.
My question is: How can I return it to the main view controller. So that I can use it for the labels in the tableViewCell.
Is my approach right? Or is there much better approach?
#teach Yes you can do it.
Create NSMutableArray in your main view controller(.H).Suppose you have
NSMutableArray *id1,*name1,*date1,*branch1;
now you can add it in your sql variables like this
NSString *querySQL = #"select *yourcolumnnames from inquiry_tb where=?";
NSNumber *id = [[NSNumber alloc] initWithInt:sqlite3_column_int(statement, 0)];
[id1 addObject:id];
NSString *name = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 1)];
[name1 addObject:name];
NSString *date = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 2)];
[date1 addObject:date];
NSString *branch = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement,3 )];
[branch1 addObject:branch];
after that you can add your NSMutableArray *id1,*name1,*date1,*branch1; in your UITableViewcell and show the NSMutableArray.
Before the property has to assign(.H)
#property (strong, nonatomic) UILabel *id1label;
Now in (.m)
id1label=[[UILabel alloc]init];
id1label.frame=CGRectMake(115, 5, 400, 50);
id1label.font=[UIFont fontWithName: #"Neue Helvetica" size:14.0];
id1label.lineBreakMode = NSLineBreakByWordWrapping;
id1label.numberOfLines=0;
id1label.textColor=[UIColor blueColor];
NSArray *comment=[ youranotherNSMutableArray objectAtIndex:indexPath.section];
id1label.text=[comment objectAtIndex:indexPath.row];
[cell.contentView addSubview:id1label];

UITableView is not scrolling down

I created an ios app in which i have a table with dynamic cell generation. When i try to scroll down to the table. It returns to the top of the table. My code as follows:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(!self.customCell){
self.customCell = [self.tableView dequeueReusableCellWithIdentifier:#"CustomCell"];
}
//Cell configuration
int quoteIndex = indexPath.row % [vendor count];
self.customCell.description.text = message[quoteIndex];
//Cell Layout
[self.customCell.description sizeToFit];
//Height of cell
float height = (CGRectGetMaxY(self.customCell.description.frame) +5);
return height + 1;
}
Here the cellForRowAtIndexPath:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"CustomCell"];
const char *dbpath = [_databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &_beaconDB) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:
#"SELECT vendor_name, enter_message, enter_image, vendor_image, received_date, time_interval FROM beacons WHERE id=%#", [unique objectAtIndex:indexPath.row]];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(_beaconDB,
query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_ROW)
{
title = [[NSString alloc]
initWithUTF8String:
(const char *) sqlite3_column_text(
statement, 0)];
description = [[NSString alloc]
initWithUTF8String:(const char *)
sqlite3_column_text(statement, 1)];
NSString *fullImage = [[NSString alloc]
initWithUTF8String:(const char *)
sqlite3_column_text(statement, 2)];
[fullImg insertObject:fullImage atIndex:indexPath.row];
NSString *vendorImage = [[NSString alloc]
initWithUTF8String:(const char *)
sqlite3_column_text(statement, 3)];
[thumbnailImg insertObject:vendorImage atIndex:indexPath.row];
NSString *receivedDate = [[NSString alloc]
initWithUTF8String:(const char *)
sqlite3_column_text(statement, 4)];
NSString *timeInterval = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 5)];
BeaconAdTime *beaconAdTime = [[BeaconAdTime alloc] init];
NSDate *updatedTime = [beaconAdTime updatedDateTime:receivedDate andInterval:[timeInterval intValue]];
int count = 0;
do
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"MMM-dd hh:mm a"];
BeaconAdTime *beaconAdTime = [[BeaconAdTime alloc] init];
NSString *presentDateTime = [beaconAdTime presentDateTime];
NSDate *presentDateConvert = [dateFormatter dateFromString:presentDateTime];
count++;
if([updatedTime compare:presentDateConvert] == NSOrderedAscending || count == 1)
{
cell.title.text = title;
cell.description.text = description;
cell.receivedDate.text = receivedDate;
NSString *imgURL = fullImg[indexPath.row];
NSURL *imageURL = [NSURL URLWithString:imgURL];
UIImage *image = nil;
image = [UIImage imageWithData:[NSData dataWithContentsOfURL: imageURL]];
cell.vendorImage.image = image;
NSUserDefaults *userDefauts = [NSUserDefaults standardUserDefaults];
[userDefauts setObject:fullImg forKey:#"fullImage"];
[userDefauts synchronize];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:localNotif];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:[NSString stringWithFormat:#"%d", indexPath.row]];
NSString *uid = [NSString stringWithFormat:#"%d", indexPath.row];
NSDictionary *infodict = [NSDictionary dictionaryWithObject:uid forKey:#"id"];
localNotif.userInfo = infodict;
NSDate *fireTime = [[NSDate date] addTimeInterval:0];
localNotif.fireDate = fireTime;
localNotif.alertBody = description;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
if(count>1)
{
break;
}
}
}
while (updatedTime!=nil);
}
sqlite3_finalize(statement);
}
sqlite3_close(_beaconDB);
}
// cell.title.text = vendor[indexPath.row];
//
// cell.vendorImage.image = [UIImage imageNamed:#"gg.jpg"];
return cell;
}
Thanks in advance.
You should not dequeue cells in height for row at index path. Also, self.customCell implies that you are working with just one cell. This is a very strange and unusual pattern.
Also, all those sqlite calls for each recycled cell is very inefficient. Instead, fetch the data you need into an array and work with that.
Also, avoid allocating formatters etc. in your cell method. Instead, use a static variable.

[__NSCFString _isResizable]: unrecognized selector sent to instance 0xa7a52e0

I have image URL strings in database. Then I retrieving the images and add to productimg_array. I need to show the productimg_array images to UITableView cell. I'm using imageView.image=[UIImage imageNamed:[productimg_array objectAtIndex:indexPath.row]];
But app crashing.
const char *sql = "SELECT id,cat_id,product_image,order_by,description FROM product";
NSLog(#"sql is %s",sql);
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
// We "step" through the results - once for each row.
while (sqlite3_step(statement) == SQLITE_ROW) {
product_image = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 2)];
// NSLog(#"product_image is %#",product_image);
[productimg_array addObject:product_image];
}
}
TableView:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"productimg_array is %lu",(unsigned long)[productimg_array count]);
return [productimg_array count];
}
- (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];
}
for(int i=0; i<[productimg_array count];i++){
[cell.imageView setImageWithURL:[NSURL URLWithString:[productimg_array objectAtIndex:i]]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
cell.imageView.image = [productimg_array objectAtIndex:indexPath.row];
here cell.imageView.image expect an image in the position but the array has a set of strings that you are passing to set in the imageview
cell.imageView.image =[UIImage imageNamed:[productimg_array objectAtIndex:indexPath.row]];
Note :
product_image = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];
can crash if the product_image is empty or null in db,so do this
char *nameChars = (char *) sqlite3_column_text(statement, 2);
if (nameChars) {
product_image = [[NSString alloc] initWithUTF8String:nameChars];
}
First you need to download that images from URL location and then you have to assign it to your image view.For this you can use lazy loading.

SQLite and UITableView: Crash after numberOfRowsInSection

First I would like to make clear that I do not want to use Core Data in this case.
My Problem: whenever my tableview's delegate methodd: numberOfRowsInSection is called, the application crashes with nothing more than an (lldb) error message.
First I have two classes which helps me retrieve rows from my SQLite database.
sqlite.m:
#import "sqlite.h"
#import <sqlite3.h>
#implementation sqlite
#synthesize carbohydrates = _carbohydrates;
#synthesize name = _name;
#synthesize fat = _fat;
#synthesize kcal = _kcal;
- (id)initWithName:(NSString *)name2 carbs:(NSNumber *)carbs2
fat:(NSNumber *)fat2 kcal:(NSNumber*)kcal2 {
if ((self = [super init])) {
self.carbohydrates = carbs2;
self.name = name2;
self.fat = fat2;
self.kcal = kcal2;
}
return self;
}
- (void) dealloc {
self.name = nil;
self.kcal = nil;
self.fat = nil;
self.carbohydrates = nil;
[super dealloc];
}
#end
NutritionsDB.m:
#import "NutritionsDB.h"
#import "sqlite.h"
#implementation NutritionsDB
static NutritionsDB *_database;
+ (NutritionsDB*)database {
if (_database == nil) {
_database = [[NutritionsDB alloc] init];
}
return _database;
}
- (id)init {
if ((self = [super init])) {
NSString *sqLiteDb = [[NSBundle mainBundle] pathForResource:#"Livsmedeldatabas"
ofType:#"sqlite"];
if (sqlite3_open([sqLiteDb UTF8String], &_database) != SQLITE_OK) {
NSLog(#"Failed to open database!");
}
}
return self;
}
- (void)dealloc {
sqlite3_close(_database);
[super dealloc];
}
-(void)setUpDB{
const char *sql_stmt = "CREATE INDEX IF NOT EXISTS name_index on livsmedel(namn)";
char *errMsg;
if (sqlite3_exec(_database, sql_stmt, NULL, NULL, &errMsg) == SQLITE_OK)
{
puts("Index successfully created");
// SQL statement execution succeeded
}
}
-(NSArray*)sqliteInfo:(NSString*)predicateString{
NSMutableArray *retval = [[[NSMutableArray alloc] init] autorelease];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(_database, [predicateString cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, nil)
== SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
char *nameChars = (char *) sqlite3_column_text(statement, 0);
char *kcalchars = (char *) sqlite3_column_text(statement, 1);
char *fatchars = (char *) sqlite3_column_text(statement, 2);
char *carbchars = (char *) sqlite3_column_text(statement, 3);
NSLog(#"\n%s, \n%s, \n%s", kcalchars, fatchars, carbchars);
NSString *name = [[NSString alloc] initWithUTF8String:nameChars];
NSString *kcal = [NSString stringWithFormat:#"%s", kcalchars];
NSString *fat = [NSString stringWithFormat:#"%s", fatchars];
NSString *carb = [NSString stringWithFormat:#"%s", carbchars];
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * kcalNumber = [f numberFromString:kcal];
NSNumber * fatNumber = [f numberFromString:fat];
NSNumber * carbNumber = [f numberFromString:carb];
NSLog(#"\n kcalnum : %#, \n fatnum: %#, \n carbnum : %#", kcalNumber, fatNumber, carbNumber);
[f release];
sqlite *info = [[sqlite alloc] initWithName:name carbs:carbNumber fat:fatNumber kcal:kcalNumber] ;
NSLog(#"%#,%#,%#,%#", name, kcal, fat, carb);
[retval addObject:info];
[name release];
[kcal release];
[fat release];
[carb release];
}
sqlite3_finalize(statement);
}
return retval;
}
#end
Now, In one of my viewcontrollers, I continuously make database calls via the method
-(NSArray*)sqliteInfo:(NSString*)predicateString.
predicateString is a copy of a text which the user enters in a search bar inside the VC.
In addition to the UISearchBar, the VC also holds a UITableView to represent the rows fetched from the databse.
Here is how I update the datasource contents and re-render the the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.nutritionList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"hej");
static NSString *CellIdentifier = #"NutritionIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.font = [UIFont fontWithName:#"Avenir-Medium" size:18.0];
cell.textLabel.text = [(sqlite*)[self.nutritionList objectAtIndex:indexPath.row] name];
return cell;
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
[self.nutritionList removeAllObjects];
if (searchText.length < 2) {
;
} else {
NSString *firstChar = [searchText substringWithRange:NSMakeRange(0, 1)];
if([firstChar isEqualToString:#"å"] ||
[firstChar isEqualToString:#"Å"] ||
[firstChar isEqualToString:#"ä"] ||
[firstChar isEqualToString:#"Ä"] ||
[firstChar isEqualToString:#"ö"] ||
[firstChar isEqualToString:#"Ö"]){
NSString *lowerString = [firstChar lowercaseString];
NSString *upperString = [firstChar uppercaseString];
NSString *newSearchText = [searchText stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:lowerString];
NSString *newSearchText_upper = [searchText stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:upperString];
NSString *formatString = [[[[[#"'"stringByAppendingString:newSearchText]stringByAppendingString:#"%'"]
stringByAppendingString:#" OR namn like'"]stringByAppendingString:newSearchText_upper]stringByAppendingString:#"%' ORDER BY namn COLLATE NOCASE"];
NSArray *array = [[NutritionsDB database] sqliteInfo:[NSString stringWithFormat:#"Select * from livsmedel where namn like %#", formatString ]];
NSLog(#"%#",[NSString stringWithFormat:#"Select * from livsmedel where namn like %#", formatString ] );
for (sqlite *info in array) {
[self.nutritionList addObject:info];
}
}else{
NSString *formatString = [[#"'"stringByAppendingString:searchText]stringByAppendingString:#"%' "];
NSArray *array = [[NutritionsDB database] sqliteInfo:[NSString stringWithFormat:#"SELECT * FROM livsmedel WHERE namn LIKE %#", formatString ]];
NSLog(#"%#",[NSString stringWithFormat:#"Select * from livsmedel where namn like %#", formatString ] );
for (sqlite *info in array) {
[self.nutritionList addObject:info];
NSLog(#"carbs %#, fat %#, kcal %#", info.carbohydrates, info.fat, info.kcal);
}
}
}
[self.tableView reloadData];
}
Finally the crash occurs after the numberOfRowsInSection method is called, but before the cellForRowAtIndexPath is called. Somewhere between them.
The console says (lldb), nothing else. The view below is the assembly instructions that indicates where the crash occurs. However, I do not now how to interpret them.
My question: Why does this crash occur?
Thanks for your help.

UITableView stops scrolling

My TableView, which uses custom cells, sometimes 'stops' scrolling when I return from a DetailView.
I can push the view up a couple inches or so, and the lower cells do come into view, but the scrollview should be 4 or 5 windows in height.
The detail view is called through a segue. In addition to the back button, I also trigger a return with a swipe gesture. Either way it happens. - not all the time though.
Hope this is enough info. Does anyone have any ideas what I am screwing up?
Thanks!
Here's some code:
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
int row = [indexPath row];
int sect = [indexPath section];
NSDictionary *shift = [globals.shifts objectAtIndex:sect];
NSLog(#"\nShift = %#", shift);
NSLog(#"\nEmployees = %#", [shift objectForKey:#"Employees"]);
NSArray *employees = [shift objectForKey:#"Employees"];
NSDictionary *employee = [employees objectAtIndex:row];
globals.badge = [employee objectForKey:#"Badge"];
globals.name = [employee objectForKey:#"Name"];
return indexPath;
}
- (void)receivedData:(NSData *)responseData {
NSError* error;
globals.report = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
globals.shifts = [globals.report objectForKey:#"shifts"];
[globals.ai removeFromSuperview];
[table reloadData];
self.title = [ [NSString alloc] initWithFormat:#"%# - %#",
[globals.clientNames objectAtIndex:globals.curClient],
[self.dateFormatter stringFromDate: globals.weekEnd]];
self.title = [ [NSString alloc] initWithFormat:#"%#",
[self.dateFormatter stringFromDate: globals.weekEnd]];
}
-(void) viewWillAppear:(BOOL)animated {
if( globals == nil || globals.report == nil )
{
appDelegate = (MSIAppDelegate *)[[UIApplication sharedApplication] delegate];
globals = [appDelegate globals];
[[self view] addSubview:globals.ai];
NSString *urlString = [[NSString alloc] initWithFormat:
#"http://www.msiwebtrax.com/Client/%#/WeeklyReport?id=%#&start=%#&end=%#", [globals.clientIDs objectAtIndex:globals.curClient], globals.userName, [globals.formatter stringFromDate: globals.weekEnd], [globals.formatter stringFromDate: [globals.weekEnd dateByAddingTimeInterval:(-7) * 24 * 60 * 60] ]];
NSURL *url = [NSURL URLWithString: urlString];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^{
NSData* data = [NSData dataWithContentsOfURL:
url];
[self performSelectorOnMainThread:#selector(receivedData:)
withObject:data waitUntilDone:YES];
});
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollView *tempScrollView=(UIScrollView *)self.view;
NSLog(#"scroll view dimensions: %f, %f", tempScrollView.contentSize.height, tempScrollView.contentSize.width);
self.dateFormatter = [[NSDateFormatter alloc] init];
[self.dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[self.dateFormatter setTimeStyle:NSDateFormatterNoStyle];
// Uncomment the following line to preserve selection between presentations.
self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
Here's the number of sections/rows
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
int sections = [globals.shifts count];
return sections;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSDictionary* shift = [globals.shifts objectAtIndex:section];
int rowCount = [[shift objectForKey:#"Employees"] count];
return rowCount;
}
--
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"EmployeeHoursCell";
EmployeeHoursCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if( cell == nil ) {
cell = [[EmployeeHoursCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
globals.employees = [[globals.shifts objectAtIndex:section] objectForKey:#"Employees"];
cell.Row.text = [[NSString alloc] initWithFormat:#"%3d", (row+1)];
cell.Id.text = [[globals.employees objectAtIndex:row] objectForKey:#"Badge"];
cell.Name.text = [[globals.employees objectAtIndex:row] objectForKey:#"Name"];
NSArray *days = [[globals.employees objectAtIndex:0] objectForKey:#"Days"];
cell.Hrs1.text = [[NSString alloc] initWithFormat:#"%#",[[days objectAtIndex:0] objectForKey:#"Rounded"] ];
cell.Hrs1.text = [[NSString alloc] initWithFormat:#"%.2f", [cell.Hrs1.text doubleValue]];
cell.Hrs2.text = [[NSString alloc] initWithFormat:#"%#",[[days objectAtIndex:1] objectForKey:#"Rounded"] ];
cell.Hrs2.text = [[NSString alloc] initWithFormat:#"%.2f", [cell.Hrs2.text doubleValue]];
cell.Hrs3.text = [[NSString alloc] initWithFormat:#"%#",[[days objectAtIndex:2] objectForKey:#"Rounded"] ];
cell.Hrs3.text = [[NSString alloc] initWithFormat:#"%.2f", [cell.Hrs3.text doubleValue]];
cell.Hrs4.text = [[NSString alloc] initWithFormat:#"%#",[[days objectAtIndex:3] objectForKey:#"Rounded"] ];
cell.Hrs4.text = [[NSString alloc] initWithFormat:#"%.2f", [cell.Hrs4.text doubleValue]];
cell.Hrs5.text = [[NSString alloc] initWithFormat:#"%#",[[days objectAtIndex:4] objectForKey:#"Rounded"] ];
cell.Hrs5.text = [[NSString alloc] initWithFormat:#"%.2f", [cell.Hrs5.text doubleValue]];
cell.Hrs6.text = [[NSString alloc] initWithFormat:#"%#",[[days objectAtIndex:5] objectForKey:#"Rounded"] ];
cell.Hrs6.text = [[NSString alloc] initWithFormat:#"%.2f", [cell.Hrs6.text doubleValue]];
cell.Hrs7.text = [[NSString alloc] initWithFormat:#"%#",[[days objectAtIndex:6] objectForKey:#"Rounded"] ];
cell.Hrs7.text = [[NSString alloc] initWithFormat:#"%.2f", [cell.Hrs7.text doubleValue]];
return cell;
}
Okay all - in the viewWillAppear method, I tried reloading the table, which didn't have any impact. But I then reset the contentSize to 0,0 before and it seems to work fine now, not entirely sure why - but I noticed the first time I come back from a detailview, the height is set to around 2000, the second time I return it is zero - at which point it works. So I set it to zero and reload.
Thanks Darren and all for the responses!

Resources