Tapping on UIImageView and opening a bigger Image - ios

I have an app that displays, in a tableview, an image and some details. I'm trying to make it so that, when someone touches on the image displayed in my table view, the app opens the same image in a bigger size, with the ability to navigate between them.
This is my current code:
singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imgTapped:)];
singleTap.numberOfTapsRequired =1;
singleTap.numberOfTouchesRequired = 1;
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [json count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyCell"];
if(cell == nil){
[[NSBundle mainBundle] loadNibNamed:#"AgendaCell" owner:self options:nil];
cell = self.agendaCell;
}
agendaCell.dateCell.text = [[json objectAtIndex:[indexPath row]] objectForKey:#"date"];
agendaCell.enderecoCell.text = [[json objectAtIndex:[indexPath row]] objectForKey:#"endereco"];
agendaCell.tituloCell.text = [[json objectAtIndex:[indexPath row]] objectForKey:#"titulo"];
[agendaCell.imgCell setImageWithURL:[NSURL URLWithString:#"http://sallescds.com.br/wp-content/uploads/2012/12/xepop-300x300.jpg"] placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
[agendaCell.imgCell setUserInteractionEnabled:YES];
[agendaCell.imgCell addGestureRecognizer:singleTap];
return cell;
}
Here is the function when the image is tapped:
-(void)imgTapped:(UIGestureRecognizer *)gestureRecognizer{
NSLog(#"detectado o click no UIImageView BITCH!");
IMGDetailViewController *imgView = [[IMGDetailViewController alloc] initWithNibName:#"IMGDetailViewController" bundle:nil];
[self.navigationController pushViewController:imgView animated:YES];
}
When I try this, the app just crashes.

I don't know how you're setting agendaCell, but usually the line that says:
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"AgendaCell" owner:self options:nil];
cell = self.agendaCell;
}
would be:
if (cell == nil) {
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"AgendaCell" owner:self options:nil];
cell = [nibObjects objectAtIndex:0];
}
See How do you load custom UITableViewCells from Xib files?

Related

iOS TableView Loading Same Cell

I am using the following code to have a tab view. When you click one tab it expands downwards, this is currently working okay but if I open the first tab with the indexPath of 0 which should load a separate custom opened view it will load but then when I go to the other indexPath tabs it displays the same view but if I reload the app and go to the other tabs first they load the correct view. It's nearly like the app is making a cache?
Is there any way to solve this??
Thanks
- (void)viewDidLoad {
row = 1000;
NSLog(#"row is %d", row);
[super viewDidLoad];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
infoStory = [userDefaults objectForKey:#"storyname"];
// Do any additional setup after loading the view.
numbers = [NSArray arrayWithObjects:#"1", #"2", #"3", #"4", #"5", #"6", #"7", #"8", nil];
// Initialize thumbnails
text = [NSArray arrayWithObjects: #"Arriving by Bus or Train", #"Arriving by Car (Autobahn 5)", #"Arriving by Car (Autobahn 6)", #"Wifi", #"Registration", #"Refreshments", #"Evening Event", #"Contact Information", nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(row == indexPath.row){
if(indexPath.row == 0){
static NSString *simpleTableIdentifier2 = #"TableViewCellOPENEDbustaxi2";
TableViewCell1 *cell = (TableViewCell1 *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier2];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"TableViewCellOPENEDbustaxi2" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.text.text = [text objectAtIndex:indexPath.row];
return cell;
}
else{
static NSString *simpleTableIdentifier = #"TableViewCellOPENED";
TableViewCell1 *cell2 = (TableViewCell1 *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell2 == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"TableViewCellOPENED" owner:self options:nil];
cell2 = [nib objectAtIndex:0];
}
cell2.text.text = [text objectAtIndex:indexPath.row];
return cell2;
}
}
else{
static NSString *simpleTableIdentifier = #"TableViewCellINFO";
TableViewCell1 *cell = (TableViewCell1 *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"TableViewCellINFO" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.text.text = [text objectAtIndex:indexPath.row];
return cell;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [numbers count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(row == indexPath.row){
row = 1000;
NSLog(#"row is %d", row);
[self.tableViewObject reloadData];
}
else{
row = indexPath.row;
NSLog(#"row is %d", row);
[self.tableViewObject reloadData];
}
}
#end
I think the problem might be that you did not specify the correct reuse identifier in the attributes inspector of the cell in your XIB file(s). You should check if you use the wrong one somewhere, so dequeueing will return an unexpected cell.
You could also set the appropriate reuseIdentifier manually, with a little setValue:forKey: trick:
static NSString *simpleTableIdentifier2 = #"TableViewCellOPENEDbustaxi2";
TableViewCell1 *cell = (TableViewCell1 *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier2];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"TableViewCellOPENEDbustaxi2" owner:self options:nil];
cell = [nib objectAtIndex:0];
[cell setValue:simpleTableIdentifier2 forKey:#"reuseIdentifier"];
}
and so on for the other cells and identifiers.

Activity indicator not hiding

I want to use activity indicator to show loading activity, like this:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//Create cell
static NSString *cellIdentifier = #"cell";
MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MyCellView" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
// Check condition, either we got objects
if (!isDataLoaded){
cell.myActivityIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
cell.myActivityIndicator.center=self.view.center;
[cell.myActivityIndicator startAnimating];
[self.view addSubview:cell.myActivityIndicator];
NSLog(#"Current loading");
} else {
cell.myActivityIndicator.hidden = YES;
[cell.myActivityIndicator removeFromSuperview];
}
return cell;
}
However, its not vanish, even when i know that "else" block is executed. How to hide it?

First 3 cells are the same but turn correct once I start scrolling?

So Whenever I launch my app it looks like this and the first 3 cells are all the same. But once I start scrolling up and down it fixes and cell 2 and 3 show the correct information.
This is currently how my code looks like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"MyFeedCell";
SampleTableCell *cell = (SampleTableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"SampleTableCell" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (SampleTableCell *)currentObject;
break;
}
}
}
// Configure the cell.
NSUInteger row = [indexPath row];
NSUInteger count = [_allEntries count];
RSSEntry *entry = [_allEntries objectAtIndex:(count-row-1)];
NSString *imageString = entry.image;
imageString = [imageString stringByReplacingOccurrencesOfString:#"128x67" withString:#"768x432"];
NSLog(#"IMAGE : %#", imageString);
NSURL *imgURL = [[NSURL alloc] initWithString:imageString];
[cell.profilePicture setImageWithURL:imgURL placeholderImage:[UIImage imageNamed:nil]];
NSString *date = [entry.articleDate substringToIndex:12];
cell.datePosted.text = date;
cell.name.text = entry.articleTitle;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Here's a gif of how it's currently functioning
http://gyazo.com/2011099496257445c008a717beabd8fd
The whole topLevelObjects gambit is no longer necessary, replace your code with this and see if your still getting the problem:
//this above #interface
static NSString *CellIdentifier = #"MyFeedCell";
//Put this in viewDidLoad
[self.table registerClass:[SampleTableCell class] forCellReuseIdentifier:CellIdentifier];
[self.table registerNib:[UINib nibWithNibName:#"SampleTableCell" bundle:nil] forCellReuseIdentifier:CellIdentifier]
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
SampleTableCell *cell = (SampleTableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell.
NSUInteger row = [indexPath row];
NSUInteger count = [_allEntries count];
RSSEntry *entry = [_allEntries objectAtIndex:(count-row-1)];
NSString *imageString = entry.image;
imageString = [imageString stringByReplacingOccurrencesOfString:#"128x67" withString:#"768x432"];
NSLog(#"IMAGE : %#", imageString);
NSURL *imgURL = [[NSURL alloc] initWithString:imageString];
[cell.profilePicture setImageWithURL:imgURL placeholderImage:[UIImage imageNamed:nil]];
NSString *date = [entry.articleDate substringToIndex:12];
cell.datePosted.text = date;
cell.name.text = entry.articleTitle;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}

releasing thread1 exc_bad_access

I am new at IOS programing and I have program that works fine, but I found out that it has memory leek, so I start releasing object.
When I now start the program it give me an error:
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
and :
Thread 1: EXC_BAD_ACCESS (code = 1, address = 0x3f800010)
I tried to debug it and I found out that program crashed at creating tableView.
It create whole first section, but in the second row in second section it crashed in the returning line.
here is my code of creating table:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView setBackgroundView:nil];
tableView.backgroundColor = [UIColor clearColor];
// Configure the cell...
if ([self tableView:tableView canCollapseSection:indexPath.section])
{
if (!indexPath.row)
{
static NSString *CellIdentifier = #"TitleCell";
UILabel *title;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = nil;
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"TitleCell" owner:self options:nil];
titleCell.layer.masksToBounds = YES;
titleCell.layer.cornerRadius =0.0;
cell = titleCell;
self.titleCell = nil;
}
title =(UILabel *)[cell viewWithTag:1];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
NSString *orderString=[[[NSString alloc] init] autorelease];
title.text = [[titles objectAtIndex:indexPath.section] objectAtIndex:2];
cell.accessoryView = nil;
if (!isPad()) {
[cell.contentView setBackgroundColor:[UIColor colorWithRed:0.0 green:0.18 blue:0.24 alpha:1]];
}
return cell;
}
else
{
// all other rows
static NSString *CellIdentifier = #"DataCell";
UILabel *title;
UILabel *update;
UILabel *download;
UILabel *updateText;
UILabel *downloadText;
UIImageView *favoriteIcon;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = nil;
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"DataCell" owner:self options:nil];
cell = dataCell;
self.dataCell = nil;
}
cell.layer.cornerRadius =0;
title =(UILabel *)[cell viewWithTag:1];
download = (UILabel *)[cell viewWithTag:3];
update = (UILabel *)[cell viewWithTag:2];
favoriteIcon = (UIImageView *)[cell viewWithTag:4];
updateText = (UILabel *)[cell viewWithTag:5];
downloadText = (UILabel *)[cell viewWithTag:6];
updateText.text = NSLocalizedString(#"updated", nil);
downloadText.text = NSLocalizedString(#"downloaded", nil);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM. d. YYYY"];
starIcone = favoriteIcon;
int indicator = 0;
for (int i=0; i<[allData count]; i++) {
if ([[[allData objectAtIndex:i] objectAtIndex:1] isEqualToNumber:[[titles objectAtIndex:indexPath.section] objectAtIndex:0]] ) {
indicator++;
}
if (indicator == indexPath.row) {
title.text = [[allData objectAtIndex:i] objectAtIndex:2];
download.text = [dateFormat stringFromDate:[self db_get_date:[[[allData objectAtIndex:i] objectAtIndex:0]intValue]]];
update.text = [dateFormat stringFromDate:[[allData objectAtIndex:i] objectAtIndex:3]];
break;
}
}
[dateFormat release];
[favoriteIcon setAccessibilityHint:title.text];
if ([favorits count]==0) {
favoriteIcon.image = [[UIImage alloc]
initWithContentsOfFile:
[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:
#"blankstar.png"]];
}
for (int i=0; i<[favorits count]; i++) {
if ([title.text isEqualToString:[[favorits objectAtIndex:i] objectAtIndex:2]]) {
favoriteIcon.image = [[UIImage alloc]
initWithContentsOfFile:
[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:
#"star.png"]];
break;
}
else {
favoriteIcon.image = [[UIImage alloc]
initWithContentsOfFile:
[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:
#"blankstar.png"]];
}
}
UITapGestureRecognizer *recognizer = [[[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(AddIcone:)]autorelease
];
[favoriteIcon setUserInteractionEnabled:YES];
[favoriteIcon addGestureRecognizer:recognizer];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
return cell;
}
}
return nil;
I also tried just putting :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TitleCell";
UILabel *title;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = nil;
if (cell == nil) {
self.titleCell = [[[NSBundle mainBundle] loadNibNamed:#"TitleCell" owner:self options:nil]objectAtIndex:0];
titleCell.layer.masksToBounds = YES;
titleCell.layer.cornerRadius =0.0;
cell = titleCell;
self.titleCell = nil;
}
return cell;
and it crash as before.
pleas help me out and thank for your help.
[[NSBundle mainBundle] loadNibName: owner: options:] returns an array.
Replace this line with:
self.titleCell = [[[NSBundle mainBundle] loadNibNamed:#"TitleCell" owner:self options:nil] objectAtIndex:0];

Issue with custom TableViewCells in table display from NSMutableArray

I'm having a weird issue - in my code I am fetching data from the internet in XML format and fill a NSMutableArray (altogether 34 items).
This data should be displayed in a TableView. I created a custom TableViewCell for that. However it always only displays the first 10 items (0-9), then the 11th item will change as you scroll up the table (starting from 11 and changing itself to 12, 13, 14 ...), then it will display the first 10 items again.
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
return [myCurrencies count];
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *myIdentifier = #"CurrencyDisplayCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"CurrencyTableViewCell" owner:self options:nil];
cell = tableViewCell;
cell.accessoryType = UITableViewCellAccessoryNone;
self.tableViewCell = nil;
}
NSLog(#"%i",indexPath.row);
actualRate.text = [NSString stringWithFormat:#"%#",[myRates objectAtIndex:indexPath.row]];
currencyCode.text = [NSString stringWithFormat:#"(%i) %#",indexPath.row,[myCurrencies objectAtIndex:indexPath.row]];
countryFlag.image = [UIImage imageNamed:[NSString stringWithFormat:#"%#.gif",[myCurrencies objectAtIndex:indexPath.row]]];
return cell;
}
However when I just set the textlabel of the standard cell everything is fine and I am displayed 34 table rows with numbers 0 - 33:
cell.textLabel.text = [NSString stringWithFormat:#"%i",indexPath.row];
Any idea out there?
your cell initialization is wrong
first of all name all your labels and image views in Interface Builder with tag
for example actualRate - tag 101
currencyCode tag 102
countryFlag tag 103
then
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *myIdentifier = #"CurrencyDisplayCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier];
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:#"CurrencyTableViewCell" owner:self options:nil] lastObject];
}
NSLog(#"%i",indexPath.row);
[((UILabel *)[cell viewWithTag:101]) setText:[NSString stringWithFormat:#"%#",[myRates objectAtIndex:indexPath.row]]];
[((UILabel *)[cell viewWithTag:102]) setText:[NSString stringWithFormat:#"(%i) %#",indexPath.row,[myCurrencies objectAtIndex:indexPath.row]]];
[((UIImageView *)[cell viewWithTag:103]) setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#.gif",[myCurrencies objectAtIndex:indexPath.row]]]];
return cell;
}

Resources