slider in tableview full demo - slider

for CustomeTableViewCell.h
//
// CustomeTableViewCell.h
// Slider Program
//
// Created by Naeem Shaikh on 21/08/14.
// Copyright (c) 2014 KIintu Designs Pvt. Ltd. All rights reserved.
//
#import <UIKit/UIKit.h>
#protocol SliderDelegate<NSObject>
- (void)sliderChanged:(id)self;
#end
#interface CustomeTableViewCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UILabel *myCellLabel;
#property (weak, nonatomic) IBOutlet UISlider *mySlider;
#property (weak, nonatomic) IBOutlet UIButton *btnCell;
#property (weak, nonatomic) id <SliderDelegate>sliderDelegate;
- (IBAction)sliderValuechanged:(UISlider *)sender;
#end
for CustomeTableViewCell.m
//
// CustomeTableViewCell.m
// Slider Program
//
// Created by Naeem Shaikh on 21/08/14.
// Copyright (c) 2014 KIintu Designs Pvt. Ltd. All rights reserved.
//
#import "CustomeTableViewCell.h"
#implementation CustomeTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)awakeFromNib
{
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (IBAction)sliderValuechanged:(UISlider *)sender {
self.myCellLabel.text = [NSString stringWithFormat:#"%d",(NSInteger)sender.value];
//call the custom delegate each time when slider is slided
if([_sliderDelegate respondsToSelector:#selector(sliderChanged:)])
{
[_sliderDelegate sliderChanged:self]; //passing the entire cell itself
}
}
#end
for viewcontroller.h
#import <UIKit/UIKit.h>
#import "CustomeTableViewCell.h"
#interface ViewController : UIViewController <UITableViewDataSource , UITableViewDelegate>{
//NSArray *tableList;
UITableView *mytableview;
int SliderChangeValue;
}
#property (strong , nonatomic) IBOutlet UIView *tableDemo;
#property (strong , nonatomic) NSMutableArray *arrSlider;
#property (strong, nonatomic) NSMutableDictionary *sliderDicValues;
#property (weak, nonatomic) IBOutlet UITableView *myTableView;//add outlet to tableview
#end
for viewcontroller.m
//
// ViewController.m
// Slider Program
//
// Created by Naeem Shaikh on 21/08/14.
// Copyright (c) 2014 KIintu Designs Pvt. Ltd. All rights reserved.
//
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize arrSlider;
#synthesize sliderDicValues;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// tableList = [NSArray arrayWithObjects:
// #"Cell 1",#"Cell 2",#"Cell 3",#"Cell 4",#"Cell 5",
// #"Cell 6",#"Cell 7",#"Cell 8",#"Cell 9",#"Cell 10",
// #"Cell 11",#"Cell 12",#"Cell 13",#"Cell 14",#"Cell 15",
// #"Cell 16",#"Cell 17",#"Cell 18",#"Cell 19",#"Cell 20",
// nil];
arrSlider = [[NSMutableArray alloc]init];
sliderDicValues = [[NSMutableDictionary alloc]init];
//[mytableview registerClass:[CustomeTableViewCell class] forCellReuseIdentifier:#"Cell"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//[tableList count]
return 50;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableCell = #"Cell";
CustomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableCell];
if (cell == nil) {
cell = [[CustomeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableCell];
}
if([self.sliderDicValues objectForKey:[NSString stringWithFormat:#"%d",indexPath.row]]) //check if there is any slided value is present
{
NSNumber *value = [self.sliderDicValues objectForKey:[NSString stringWithFormat:#"%d",indexPath.row]];
[cell.mySlider setValue:value.integerValue]; //set the slider value
[cell.myCellLabel setText:[NSString stringWithFormat:#"%d",value.integerValue]];//and also label
}
else //set to default values
{
[cell.mySlider setValue:(NSInteger)0];
[cell.myCellLabel setText:#"label"];
}
//add a single target don't add double target to slider
cell.sliderDelegate = self;//set the delegate
return cell;
}
- (void)sliderChanged:(CustomeTableViewCell *)cell
{
NSIndexPath *path = [_myTableView indexPathForCell:cell]; //get the indexpath
if(path)//check if valid path
{
SliderChangeValue = cell.mySlider.value;
[self.sliderDicValues setObject:[NSNumber numberWithInt:SliderChangeValue] forKey:[NSString stringWithFormat:#"%d",path.row]]; //set the value in the dictionary later used in the cellForRowAtIndexPath method
}
// SliderChangeValue = (int)sender.value;
NSLog(#"%d",SliderChangeValue);
}
//dont use it
-(void)customSliderValue:(UISlider *)sender{
// NSString *value =[NSString stringWithFormat:#"%d" ,(int)sender.value];
// NSString *tag = [NSString stringWithFormat:#"%d", (int)sender.tag];
//
// NSLog(#"%# %#",value , tag);
//
// [self.dicSilder setObject:value forKey:#"value"];
// [self.dicSilder setObject:tag forKey:#"tag"];
//
// [self.arrSlider addObject:self.dicSilder];
// NSLog(#"%#",self.arrSlider);
SliderChangeValue = (int)sender.value;
NSLog(#"%d",SliderChangeValue);
}
//this is also put a delegate from the cell like slider , just add the another method in the protocol and perform action, if u don't get just comment i will update the code and u hav t modify this method according to your requirement
-(void)customeBtnClicked:(UIButton *)sender
{
NSString *value =[NSString stringWithFormat:#"%d" ,SliderChangeValue];
NSString *tag = [NSString stringWithFormat:#"%d", sender.tag];
//NSLog(#"%# %#",value,tag);
NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
[dic setObject:value forKey:#"value"];
[dic setObject:tag forKey:#"tag"];
//NSLog(#"%#",dic);
[arrSlider addObject:dic];
NSLog(#"%#",arrSlider);
NSString *sliderTagAtIndexPath = #"";
//NSString *sliderValueAtindexPath = #"";
for (int i = 0; i < arrSlider.count; i++) {
NSString *strTag = [NSString stringWithFormat:#"%#",[[arrSlider objectAtIndex:i]valueForKey:#"tag"]];
if([strTag isEqualToString:tag])
{
//NSString *strValue = [NSString stringWithFormat:#"%#",[[arrSlider objectAtIndex:i]valueForKey:#"value"]];
sliderTagAtIndexPath = strTag;
//sliderValueAtindexPath = strValue;
}
}
UIAlertView *myAlertView = [[UIAlertView alloc]initWithTitle:#"Clicked"
message:[NSString stringWithFormat:#"Cell : %# Value: %d", sliderTagAtIndexPath ,SliderChangeValue]
//message:[NSString stringWithFormat:#"Cell : %# Value: %#", sliderTagAtIndexPath ,sliderValueAtindexPath]
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[myAlertView show];
}
#end
here is the link for whole app
https://www.mediafire.com/folder/1d4ktomjytb818l,8zcg0zbymee57z8/shared
it is demo for slider in tableview with solution.

Related

I am not able to get Navigation Bar using JSQMessagesViewController

Problem in getting Navigation Bar using JSQMessagesViewController, I have tried getting Navigation Bar in my project, but failed to get it. Please let me know what the methods need to be added to get it.
#import "JSQMessagesCollectionViewFlowLayout.h"
#import "JSQMessages.h"
#import "JSQPhotoMediaItem.h"
#import "JSQLocationMediaItem.h"
#import "JSQVideoMediaItem.h"
#import "JSQMessagesMediaViewBubbleImageMasker.h"
#import "JSQMessagesAvatarImage.h"
#import "JSQMessagesAvatarImageFactory.h"
#import "JSQMessagesBubbleImage.h"
#import "JSQMessagesBubbleImageFactory.h"
#import "UIImage+JSQMessages.h"
#import "Chat.h"
#interface ChatpageViewController : JSQMessagesViewController
{
NSMutableArray * fularray;
}
#property(nonatomic,strong)NSDictionary * receivedict;
#property (strong, nonatomic) IBOutlet UILabel *name;
#property (strong, nonatomic) IBOutlet UILabel *mobile;
#property (strong, nonatomic) JSQMessagesBubbleImage *outgoingBubbleImageData;
#property (strong, nonatomic) JSQMessagesBubbleImage *incomingBubbleImageData;
#end
#import "ChatpageViewController.h"
#interface ChatpageViewController ()
#end
#implementation ChatpageViewController
#synthesize receivedict,name,mobile;
//-(void)viewWillAppear:(BOOL)animated
//{
//
// // self.collectionView.collectionViewLayout.springinessEnabled = YES;
//}
(void)viewDidLoad
{
[super viewDidLoad];
fularray = [[NSMutableArray alloc] init];
// Do any additional setup after loading the view.
// [self.navigationController setNavigationBarHidden:NO animated:YES];
NSLog(#"%#",receivedict);
name.text = [receivedict objectForKey:#"Name"];
mobile.text =[receivedict objectForKey:#"Mobile"];
JSQMessagesBubbleImageFactory *bubbleFactory = [[JSQMessagesBubbleImageFactory alloc] init];
self.outgoingBubbleImageData = [bubbleFactory outgoingMessagesBubbleImageWithColor:[UIColor jsq_messageBubbleBlueColor]];
self.incomingBubbleImageData = [bubbleFactory incomingMessagesBubbleImageWithColor:[UIColor jsq_messageBubbleLightGrayColor]];
}
(id)collectionView:(JSQMessagesCollectionView *)collectionView messageBubbleImageDataForItemAtIndexPath:(NSIndexPath *)indexPath
{
JSQMessage *message = [fularray objectAtIndex:indexPath.item];
if ([message.senderId isEqualToString:self.senderId]) {
return self.outgoingBubbleImageData;
}
return self.incomingBubbleImageData;
}
(id)collectionView:(JSQMessagesCollectionView *)collectionView avatarImageDataForItemAtIndexPath:(NSIndexPath *)indexPath {
return [JSQMessagesAvatarImageFactory avatarImageWithUserInitials:#"JL" backgroundColor:[UIColor blueColor] textColor:[UIColor whiteColor] font:[UIFont systemFontOfSize:12.0] diameter:30.0];
}
(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [fularray count];
}
(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
(id)collectionView:(JSQMessagesCollectionView *)collectionView messageDataForItemAtIndexPath:(NSIndexPath *)indexPath {
return fularray[indexPath.row];
}
(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
(NSString *)senderId
{
return [receivedict objectForKey:#"Mobile"];
}
(NSString *)senderDisplayName
{
return [receivedict objectForKey:#"Name"];
}
(NSDate *)date
{
return 18/03/2016;
}
(void)didPressSendButton:(UIButton *)button withMessageText:(NSString *)text senderId:(NSString *)senderId senderDisplayName:(NSString *)senderDisplayName date:(NSDate *)date
{
[JSQSystemSoundPlayer jsq_playMessageSentSound];
JSQMessage *message = [[JSQMessage alloc] initWithSenderId:senderId
senderDisplayName:senderDisplayName
date:date
text:text];
[fularray addObject:message];
[self.collectionView reloadData];
[self finishSendingMessageAnimated:YES];
NSLog(#"%#",message);
}
Myproject is saved in the link :https://www.dropbox.com/s/ozih8ko9836fmb2/chatapp.zip?dl=0
Goto MainStoryboard->select your Controller-> in menu choose Editor-> Embed in -> Navigation Controller
Change in JSQMessagesViewController.m file in -(void)jsq_updateCollectionViewInsets
[self jsq_setCollectionViewInsetsTopValue:self.topContentAdditionalInset + 64 bottomValue:CGRectGetMaxY(self.collectionView.frame) - CGRectGetMinY(self.inputToolbar.frame)];

Using TouchesBegan in my TableViewController

Good afternoon,
I would like to tap the profile image (like Facebook app) inside a row in my TableViewController using "TouchesBegan" to display a new ViewController with more information regarding the user. That means going from TableViewController (touch in the image of the TableViewCell) and then go (segue) to another ViewController called "ProfileViewController".
When I tried to do that directly in my TableViewCell it didn't worked because it's not a ViewController (it's a subclass of TableViewController) so I cannot move to a different ViewController from that.
So, what I'm trying to do now, is create a TouchesBegan in the UIImage from my TableViewController, but I get a warning (and followed by a crash) because I use a NSArray to fill the UIImage URL and it seems that I cannot assign a TouchesBegan directly to the image.
Can you help me with more information or some example? Maybe there is something I'm missing because It's my first time trying to do something like that and any help will be appreciated.
Here you are my code:
That's my TableViewController:
//
// CarTableViewController.m
// TableViewStory
//
#import "CarTableViewController.h"
#import "CarTableViewCell.h"
#import "CarTableViewController.h"
#import "CarDetailViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>
#implementation CarTableViewController
#synthesize carMakes = _carMakes;
#synthesize carModels = _carModels;
#synthesize carImages = _carImages;
#synthesize likes = _likes;
#synthesize comments = _comments;
#synthesize username = _username;
#synthesize refuser = _refuser;
#synthesize profileImage = _profileImage;
- (void)viewDidLoad
{
[super viewDidLoad];
[self fetchJson];
[self.tableView reloadData];
// Initialize the refresh control.
self.refreshControl = [[UIRefreshControl alloc] init];
//self.refreshControl.backgroundColor = [UIColor blackColor];
//self.refreshControl.tintColor = [UIColor whiteColor];
[self.refreshControl addTarget:self
action:#selector(fetchJson)
forControlEvents:UIControlEventValueChanged];
}
- (void)viewWillAppear:(BOOL)animated
{
self.navigationController.navigationBar.hidden = YES;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [_jsonArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"carTableCell";
CarTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CarTableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.makeLabel.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:#"id"];
cell.likes.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:#"likes"];
cell.comments.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:#"comments"];
cell.username.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:#"username"];
cell.refuser.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:#"user_ref"];
cell.modelLabel.text = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:#"user"];
NSURL * imageURL = [NSURL URLWithString:[[_jsonArray objectAtIndex:indexPath.row] valueForKey:#"imagen"]];
[cell.carImage setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:#"imagen"] options:SDWebImageRefreshCached];
NSURL * imageURL2 = [NSURL URLWithString:[[_jsonArray objectAtIndex:indexPath.row] valueForKey:#"image"]];
[cell.profileImage setImageWithURL:imageURL2
placeholderImage:[UIImage imageNamed:#"image"]
options:SDWebImageRefreshCached];
return cell;
}
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
CGPoint pt = [[touches anyObject] locationInView:_profileImage];
if (pt.x>=0 && pt.x<=100 && pt.y>=0 && pt.y<=100)
{
[self performSegueWithIdentifier:#"ID" sender:self];
}
else
{
NSLog(#"image not touched");
}
}
/*
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"ShowCarDetails"])
{
CarDetailViewController *detailViewController = [segue destinationViewController];
NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];
detailViewController.carDetailModel = [[NSArray alloc]
initWithObjects:
[[_jsonArray objectAtIndex:[myIndexPath row]] valueForKey:#"date"],
[[_jsonArray objectAtIndex:[myIndexPath row]] valueForKey:#"id"],
[[_jsonArray objectAtIndex:[myIndexPath row]] valueForKey:#"imagen"],
nil];
}
}
*/
-(void)fetchJson {
self.carModels = [[NSMutableArray alloc] init];
self.carMakes = [[NSMutableArray alloc] init];
self.carImages = [[NSMutableArray alloc] init];
self.likes = [[NSMutableArray alloc] init];
self.comments = [[NSMutableArray alloc] init];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSString * urlString = [NSString stringWithFormat:#"http://website.com/service.php"];
NSURL * url = [NSURL URLWithString:urlString];
NSData * data = [NSData dataWithContentsOfURL:url];
NSError *error;
[_jsonArray removeAllObjects];
_jsonArray = [NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves
error:&error];
for(int i=0;i<_jsonArray.count;i++)
{
NSDictionary * jsonObject = [_jsonArray objectAtIndex:i];
NSString* imagen = [jsonObject objectForKey:#"imagen"];
[_carImages addObject:imagen];
NSDictionary * jsonObject2 = [_jsonArray objectAtIndex:i];
NSString* user = [jsonObject2 objectForKey:#"user"];
[_carMakes addObject:user];
NSDictionary * jsonObject3 = [_jsonArray objectAtIndex:i];
NSString* date = [jsonObject3 objectForKey:#"date"];
[_carModels addObject:date];
}
NSLog(#"carModels ==> %#", _jsonArray);
dispatch_async(dispatch_get_main_queue(), ^{
{
[self.tableView reloadData];
[self.refreshControl endRefreshing];
}});
}
);
}
#end
And that's my TableViewController.h
//
// CarTableViewController.h
// TableViewStory
//
#import <UIKit/UIKit.h>
#interface CarTableViewController : UITableViewController
#property (nonatomic, strong) IBOutlet UITableView *tableView;
#property (nonatomic, strong) NSMutableArray *carImages;
#property (nonatomic, strong) NSMutableArray *carMakes;
#property (nonatomic, strong) NSMutableArray *carModels;
#property (nonatomic, strong) NSMutableArray *likes;
#property (nonatomic, strong) NSMutableArray *comments;
#property (nonatomic, strong) NSMutableArray *username;
#property (nonatomic, strong) NSMutableArray *refuser;
#property (nonatomic, strong) NSMutableArray *profileImage;
#property (nonatomic, strong) NSMutableArray *jsonArray;
#property (nonatomic, strong) IBOutlet UIImage *touchImageVIew;
#end
Thanks in advance.
If your UIImageView should act like a button, you should make it a UIButton instead of a UIImageView. With an UIButton you can set a segue in the Interface Builder. Here's an example of the UITableViewController:
class TableViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {
let dataSource = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
dataSource.addObject(UIImage(named: "image.jpg")!)
dataSource.addObject(UIImage(named: "image.jpg")!)
tableView.reloadData()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSource.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("tableviewcell", forIndexPath: indexPath) as TableViewCell
let image = dataSource.objectAtIndex(indexPath.row) as UIImage
cell.button.setBackgroundImage(image, forState: UIControlState.Normal)
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "pushtodetail" {
println("performing segue from a button in a cell")
}
}
}

UITableview - Can't Reload data

I'm noob in Obj-c programming. I'm making an iPad application composed of a ViewController that shows me 2 text boxes , a button in the upper side , just to make a sum.
In the same Viewcontroller, i have a TableView and a Button. This Tableview initially shows me an element, but i'd want to refresh this list just clicking the button.
The function linked at the button to refresh the list ( touch down event ) is called " listaPDF " .
Viewcontroller.h
#import <UIKit/UIKit.h>
#interface VPViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
IBOutlet UITextField *txtPrimoAddendo ;
IBOutlet UITextField *txtSecondoAddendo ;
IBOutlet UILabel *lblTotale ;
IBOutlet UITableView *tabella;
NSMutableArray *lista;
}
#property (nonatomic, retain) IBOutlet UITextField *txtPrimoAddendo;
#property (nonatomic, retain) IBOutlet UITextField *txtSecondoAddendo;
#property (nonatomic, retain) IBOutlet UILabel *lblTotale;
#property (nonatomic, retain) IBOutlet UITableView *tabella;
#property (nonatomic, retain) NSMutableArray *lista;
-(IBAction)somma ;
-(IBAction)listaPDF;
#end
Viewcontroller.m
#import "VPViewController.h"
#interface VPViewController ()
#end
#implementation VPViewController
-(void)somma {
int x = [[txtPrimoAddendo text] intValue];
int y = [[txtSecondoAddendo text] intValue];
int somma = x + y ;
NSString *totale = [NSString stringWithFormat:#"La somma fa : %d", somma];
[lblTotale setText:totale];
[lblTotale setHidden:FALSE];
}
- (void)listaPDF {
int contatore = 1;
NSArray *dirFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[[NSBundle mainBundle] resourcePath] error: nil];
for (int i = 0; i < dirFiles.count ; i++) {
NSString *files = dirFiles[i];
int indice = files.length - 4 ;
NSString *extension = [files substringFromIndex:indice];
if([extension isEqual: #".pdf"]) {
NSLog(#"********* File PDF : %#", files);
[lista insertObject:files atIndex:contatore];
contatore++;
}
}
[tabella reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [lista count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"CellID";
UITableViewCell *cell = [tabella dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.imageView.image = [UIImage imageNamed:#"pdf.png"];
cell.textLabel.text = [lista objectAtIndex:indexPath.row];
return cell;
}
- (void)viewDidLoad
{
tabella.delegate = self;
tabella.dataSource = self;
tabella = [[ UITableView alloc] init];
lista = [[NSMutableArray alloc] init];
[lista insertObject:#"FIRST ELEMENT" atIndex:0];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
#synthesize lblTotale;
#synthesize txtPrimoAddendo;
#synthesize txtSecondoAddendo;
#synthesize tabella;
#synthesize lista;
#end
Thanks
I have tried your code as it is.
Why you initializing UITableView object in viewDidLoad. It doesn't need to be allocated if you are adding on Xib file. If you creating UITableView Programmatically then its required. Just comment/remove that initializing line and tableView's delegate methods will be called.
And In 'ListaPDF' function no items is being added to array 'lista', that's why no new entry was visible in UITableView.
NSMutableArray indices begin at 0 - not 1
int contatore = 1;
...
[lista insertObject:files atIndex:contatore];
contatore++;
Use addObject to add an object to a NSMutableArray
[lista addObject:files];
Remove all of your instance variables, because this:
#property (nonatomic, retain) NSMutableArray *lista;
creates an instance variable called _lista - not lista
You have quite a bit of code backwards:
tabella.delegate = self;
tabella.dataSource = self;
tabella = [[ UITableView alloc] init];
lista = [[NSMutableArray alloc] init];
You're trying to set the delegate and datasource before the table is allocated.
It should be:
tabella = [[ UITableView alloc] init];
lista = [[NSMutableArray alloc] init];
tabella.delegate = self;
tabella.dataSource = self;
However the UITableView has been added inside Interface Builder, right?
That means it's already being created for you and you shouldn't be allocating the table here.
Also: inside Interface Builder you can rightclick+drag (or ctrl+leftclick+drag) to connect your UITableView to yourUIViewController as its delegate and datasource

The first method in detail view controller is being skipped

Is there something important I need in RoomItem to ensure that this method isn't skipped? It's the very first one in my detail view controller, and it is continually skipped when I'm in debugging mode. I'm sure I'm missing something ridiculously simple, but I've been staring at it for hours and just can't figure out what it is.
#interface DetailViewController ()
- (void)configureView;
#end
#implementation DetailViewController
- (void)setDetailItem:(RoomItem *)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
}
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
[_roomTxt setText:[_detailItem room]];
[_buildingTxt setText:[_detailItem building]];
[_dateTxt setText:[self dateCreatedString]];
[_buildingImageView setImage:[_detailItem buildingImage]];
_oi = [_detailItem objectIndex];
}
}
MasterViewController (root table view) methods that alloc and init new and existing detailViewControllers
- (void)insertNewObject:(id)sender
{
//add button invokes this
DetailViewController *ivc = [[DetailViewController alloc] init];
[self.navigationController pushViewController:ivc animated:YES];
NSLog(#"detailViewController allocated and initialized: %#", ivc);
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!self.detailViewController) {
self.detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
NSLog(#"detailViewController initialized: %#", self.detailViewController);
}
//navigates to detailViewController and passes it the item's data
self.detailViewController.detailItem = [[[RoomList sharedStore] getAllItems] objectAtIndex:[indexPath row]];
[self.navigationController pushViewController:self.detailViewController animated:YES];
}
Here is the tableView:didSelectRowAtIndexPath method that should be passing everything needed by the detail view controller, from the RoomList:sharedStore:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!self.detailViewController) {
self.detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
}
//navigates to detailViewController and passes it the item's data
self.detailViewController.detailItem = [[[RoomList sharedStore] getAllItems] objectAtIndex:[indexPath row]];
[self.navigationController pushViewController:self.detailViewController animated:YES];
}
...and the RoomItem.m file:
- (void)awakeFromFetch
{
[super awakeFromFetch];
UIImage *pic = [UIImage imageWithData:[self buildingThumbnailData]];
[self setPrimitiveValue:pic forKey:#"buildingThumbnail"];
}
- (id)initWithRoom:(NSString *)room Building:(NSString *)building
{
self = [super init];
if (self) {
[self setRoom:room];
[self setBuilding:building];
}
return self;
}
DetailViewController.h
#import <UIKit/UIKit.h>
#class RoomItem;
//pic edit: added delegates
#interface DetailViewController : UIViewController <UINavigationControllerDelegate,
UIImagePickerControllerDelegate, UITextFieldDelegate, UIPopoverControllerDelegate,
UIPageViewControllerDelegate>
{
__weak IBOutlet UITextField *roomField;
__weak IBOutlet UITextField *buildingField;
__weak IBOutlet UILabel *dateLabel;
UIPopoverController *imagePickerPopover;
}
#property (nonatomic, strong) RoomItem *detailItem;
#property (weak, nonatomic) IBOutlet UIButton *updateBtn;
#property (weak, nonatomic) IBOutlet UIButton *detailsBtn;
#property (weak, nonatomic) IBOutlet UITextField *roomTxt;
#property (weak, nonatomic) IBOutlet UITextField *buildingTxt;
#property (weak, nonatomic) IBOutlet UILabel *dateTxt;
#property (weak, nonatomic) IBOutlet UIImageView *buildingImageView;
#property (weak, nonatomic) UIImage *buildingImage;
#property (weak, nonatomic) NSNumber *oi;
- (IBAction)backgroundTapped:(id)sender;
- (IBAction)takePicture:(id)sender;
- (IBAction)updateRoomItem:(id)sender;
- (IBAction)goToReportDetails:(id)sender;
#end
Edit:here is a pseudo-UML diagram that illustrates what I'm seeing when I step through with the debugger (it reads from left to right, top to bottom):
It is very strange that the setter is not being called. I would take the line
self.detailViewController.detailItem = [[[RoomList sharedStore] getAllItems] objectAtIndex:[indexPath row]];
and replace it with this code which might be a bit easier to follow:
RoomItem *tempItem = [[[RoomList sharedStore] getAllItems] objectAtIndex:[indexPath row]];
DetailViewController *tempDVC = self.detailViewController; // could just use _detailViewController
tempDVC.detailItem = tempItem; // breakpoint on this line
and then when the breakpoint hits, I would single step through the compiled assembler to find out where it goes. There are instructions for that at Xcode Debugger - how to single step at level of CPU instructions - specifically the answer a couple of weeks ago from Max MacLeod.
I'd have written this as another comment since it's not really an answer, but it's too long!
Alrighty, first things first. The reason -(void)setDetailItem:(RoomItem *)newDetailItem was being skipped is due to me not passing it the newly created RoomItem in the DVC. I was doing this initially, but mistakenly got rid of it while trying to correct the issue somewhere else.
In DetailViewController.m:
- (void)insertNewObject:(id)sender
{
RoomItem *newItem = [[RoomList sharedStore] createItem];
DetailViewController *ivc = [[DetailViewController alloc] init];
[ivc setDetailItem:newItem];
[self.navigationController pushViewController:ivc animated:YES];
NSLog(#"detailViewController allocated and initialized: %#", ivc);
}
Getting this to work though took some restructuring in the RoomList.m file. I had initially attempted to marry the create and update functions into one method. That doesn't fly if I'm creating in the MVC, and updating in the DVC.
So I split the two up:
- (RoomItem *)createItem
{
double order;
//create new roomItem
//tracks what number item it's creating
if ([allItems count] == 0) {
order = 1.0;
}
else
{
order = [[[allItems lastObject] objectIndex] doubleValue] + 1;
}
NSLog(#"Adding after %d items, order = %.2f", [allItems count], order);
RoomItem *p = [NSEntityDescription insertNewObjectForEntityForName:#"RoomItem"
inManagedObjectContext:context];
[p setObjectIndex:[NSNumber numberWithDouble:order]];
[p setDateCreated:[NSDate date]];
[allItems addObject:p];
[self saveChanges];
return p;
}
- (RoomItem *)updateItemWithRoom:(NSString *)room Building:(NSString *)building ObjectIndex:(NSNumber *)objectIndex DateCreated:(NSDate *)dateCreated ImageKey:(NSString *)imageKey BuildingImage:(UIImage *)buildingImage BuildingThumbnail:(UIImage *)buildingThumbnail BuildingThumbnailData:(NSData *)buildingThumbnailData
{
NSError *error = nil;
NSEntityDescription *entity = [NSEntityDescription entityForName:#"RoomItem" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
int32_t oid = [objectIndex integerValue] - 1;
NSLog(#"objectIndex = %d", oid);
RoomItem *currentRoomItem = [[context executeFetchRequest:request error:&error] objectAtIndex:oid];
//[currentRoomItem setItemEdited:YES];
[currentRoomItem setRoom:room];
[currentRoomItem setBuilding:building];
[currentRoomItem setImageKey:imageKey];
[currentRoomItem setBuildingImage:buildingImage];
[currentRoomItem setBuildingThumbnail:buildingThumbnail];
[currentRoomItem setBuildingThumbnailData:buildingThumbnailData];
[self saveChanges];
return currentRoomItem;
}
Lastly, the setDetailItem and configureView methods:
- (void)setDetailItem:(RoomItem *)detailItem
{
if (_detailItem != detailItem) {
_detailItem = detailItem;
[self configureView];
}
}
- (void)configureView
{
if (self.detailItem) {
[_roomTxt setText:[_detailItem room]];
[_buildingTxt setText:[_detailItem building]];
[_dateTxt setText:[self dateCreatedString]];
[_buildingImageView setImage:[_detailItem buildingImage]];
}
}
This should serve as a lesson about taking the time to read the core data documentation, or any other for that matter...especially noobs like myself.
Thanks again to emrys57 for the help.

iOS UITableView Only Reloads Cell Data When Scrolling

I am working on my first Objective-C app for iOS and am having an issue with reloading the data in a UITableView.
After reloading the data the cell content will only update when the cell is scrolled above of below the viewable area of the container.
Here is my .h code:
#import <UIKit/UIKit.h>
#import "AFHTTPClient.h"
#import "AFJSONRequestOperation.h"
#interface HelloWorldViewController : UIViewController <UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource>{
NSMutableArray *tableViewArray;
IBOutlet UITableView *tableView;
}
#property (nonatomic, retain) NSMutableArray *tableViewArray;
#property (weak, nonatomic) IBOutlet UILabel *connectionLabel;
#property (nonatomic, retain) IBOutlet UITableView *tableView;
#property (weak, nonatomic) IBOutlet UITextView *textArea;
#property (weak, nonatomic) IBOutlet UITextField *textField2;
#property (weak, nonatomic) IBOutlet UILabel *label;
#property (weak, nonatomic) IBOutlet UITextField *textField;
#property (copy, nonatomic) NSString *userName;
#property (copy, nonatomic) NSString *passWord;
#property (copy, nonatomic) NSMutableString *serverResponse;
- (IBAction)callHome:(id)sender;
#end
and .m code:
#import "HelloWorldViewController.h"
#interface HelloWorldViewController ()
#end
#implementation HelloWorldViewController
#synthesize tableViewArray;
#synthesize connectionLabel;
#synthesize userName = _userName;
#synthesize passWord = _password;
#synthesize serverResponse = _serverResponse;
#synthesize tableView;
#synthesize textArea;
#synthesize textField2;
#synthesize label;
#synthesize textField;
- (void)viewDidLoad
{
[super viewDidLoad];
tableViewArray = [[NSMutableArray alloc] init];
[tableViewArray addObject:#"TEST1"];
[tableViewArray addObject:#"TEST2"];
[tableViewArray addObject:#"TEST3"];
}
- (void)viewDidUnload
{
[self setTextField:nil];
[self setLabel:nil];
[self setTextField2:nil];
[self setTextArea:nil];
[self setTableView:nil];
[self setConnectionLabel:nil];
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
if (theTextField == self.textField) {
[theTextField resignFirstResponder];
}
return YES;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [tableViewArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.text = [self.tableViewArray objectAtIndex: [indexPath row]];
return cell;
}
- (IBAction)callHome:(id)sender {
self.userName = self.textField.text;
self.passWord = self.textField2.text;
NSMutableString *tempResponse = [[NSMutableString alloc] initWithFormat:#""];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:#"http://example.com/"]];
[client setAuthorizationHeaderWithUsername:self.userName password:self.passWord];
[client getPath:#"login.do" parameters:nil
success:^( AFHTTPRequestOperation *operation , id responseObject ){
NSLog(#"Authentication Success: %d", operation.response.statusCode);
self.serverResponse = [NSMutableString stringWithFormat:#"Authentication Success: %d", operation.response.statusCode ];
[tempResponse appendString: self.serverResponse];
self.textArea.text = tempResponse;
}
failure:^(AFHTTPRequestOperation *operation , NSError *error){
NSLog(#"Authentication Error: %#\n%#", error, operation);
}
];
[client getPath:#"test.json.do" parameters:nil
success:^( AFHTTPRequestOperation *operation , id responseObject ){
NSLog(#"Retrieval Success: %d", operation.response.statusCode);
NSDictionary *results = [operation.responseString JSONValue];
NSMutableArray *buildings = [results objectForKey:#"buildings"];
NSMutableArray *names = [[NSMutableArray alloc] init];
for (NSDictionary *building in buildings)
{
[names addObject:[building objectForKey:#"name"]];
}
self.tableViewArray = names;
self.serverResponse = [NSMutableString stringWithFormat:#"\nBuilding List Retrieval Success: %d", operation.response.statusCode ];
[tempResponse appendString: self.serverResponse];
self.connectionLabel.text = tempResponse;
}
failure:^(AFHTTPRequestOperation *operation , NSError *error){
NSLog(#"Retrieval Error: %#\n%#", error, operation);
}
];
NSLog(#"tableView is: %#", [tableView description]);
[tableView reloadData];
}
#end
When I call [self.tableView description]the result is null, but if I call it from cellForRowAtIndexPath then I get the following result:
tableView is: <UITableView: 0x8a71000; frame = (0 0; 280 191); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x6b7e860>; contentOffset: {0, 0}>. Delegate: HelloWorldViewController, DataSource: HelloWorldViewController
Here's a screenshot of interface builder:
All help is appreciated! Thanks!
You're probably not connecting the UITableView in the interface builder..
You have to drag while pressing ctrl from the file's owner to the UITableView and connect it.
Also, you should not access your properties without self, you should do:
#synthesize tableViewArray = _tableViewArray;
and then access it with:
self.tableViewArray
try to avoid accessing your ivars directly, use the property!
Good luck!
It looks like you may have not hooked up your UITableView with the HelloWorldViewController tabelView property in IB.