i am new in programming and iOS i am making an iPhone application like crud operation and inventory system i am not able to show data and fetch data in UITableView
But when Print array then this type of output produced.
2013-10-03 15:45:04.228 inventrymanagementsystem[1968:c07] name: (
"<Category: 0x7453f40> (entity: Category; id: 0x744a3b0 <x-coredata://431EF8C4-5AAE-4AA8-ADFB-A09CE5731E2D/Category/p1> ; data: <fault>)",
"<Category: 0x7456500> (entity: Category; id: 0x74501f0 <x-coredata://431EF8C4-5AAE-4AA8-ADFB-A09CE5731E2D/Category/p2> ; data: <fault>)",
"<Category: 0x7456210> (entity: Category; id: 0x7450970 <x-coredata://431EF8C4-5AAE-4AA8-ADFB-A09CE5731E2D/Category/p3> ; data: <fault>)",
"<Category: 0x7457830> (entity: Category; id: 0x743bd90 <x-coredata://431EF8C4-5AAE-4AA8-ADFB-A09CE5731E2D/Category/p4> ; data: <fault>)",
"<Category: 0x7455cf0> (entity: Category; id: 0x7438670 <x-coredata://431EF8C4-5AAE-4AA8-ADFB-A09CE5731E2D/Category/p5> ; data: <fault>)",
"<Category: 0x744b060> (entity: Category; id: 0x7438530 <x-coredata://431EF8C4-5AAE-4AA8-ADFB-A09CE5731E2D/Category/p6> ; data: <fault>)",
"<Category: 0x74496f0> (entity: Category; id: 0x7436050 <x-coredata://431EF8C4-5AAE-4AA8-ADFB-A09CE5731E2D/Category/p7> ; data: <fault>)",
"<Category: 0x744f290> (entity: Category; id: 0x744db90 <x-coredata://431EF8C4-5AAE-4AA8-ADFB-A09CE5731E2D/Category/p8> ; data: <fault>)",
"<Category: 0x744eaa0> (entity: Category; id: 0x7439d40 <x-coredata://431EF8C4-5AAE-4AA8-ADFB-A09CE5731E2D/Category/p9> ; data: <fault>)",
"<Category: 0x744fec0> (entity: Category; id: 0x74385c0 <x-coredata://431EF8C4-5AAE-4AA8-ADFB-A09CE5731E2D/Category/p10> ; data: <fault>)",
"<Category: 0x744f410> (entity: Category; id: 0x7445de0 <x-coredata://431EF8C4-5AAE-4AA8-ADFB-A09CE5731E2D/Category/p11> ; data: <fault>)",
"<Category: 0x74557c0> (entity: Category; id: 0x744ebe0 <x-coredata://431EF8C4-5AAE-4AA8-ADFB-A09CE5731E2D/Category/p12> ; data: <fault>)",
"<Category: 0x7456190> (entity: Category; id: 0x7450b30 <x-coredata://431EF8C4-5AAE-4AA8-ADFB-A09CE5731E2D/Category/p13> ; data: <fault>)",
"<Category: 0x7457010> (entity: Category; id: 0x7453180 <x-coredata://431EF8C4-5AAE-4AA8-ADFB-A09CE5731E2D/Category/p14> ; data: <fault>)",
"<Category: 0x744b740> (entity: Category; id: 0x74524b0 <x-coredata://431EF8C4-5AAE-4AA8-ADFB-A09CE5731E2D/Category/p15> ; data: <fault>)"
)
I am not able to understand what's the problem.
Code
#import "IMScategoriesViewController.h"
#import "IMSAddCategoryViewController.h"
#import "IMSAppDelegate.h"
#import "Category.h"
#interface IMScategoriesViewController ()
#property (strong) NSMutableArray *categories;
#property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
#property (nonatomic,retain) NSFetchedResultsController *fetchedresultcontroller;
#end
#implementation IMScategoriesViewController
- (NSManagedObjectContext *)managedObjectContext
{
NSManagedObjectContext *context;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:#selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
//- (void)viewDidAppear:(BOOL)animated
//{
// [super viewDidAppear:animated];
- (void)viewDidLoad
{
[super viewDidLoad];
// Fetch the devices from persistent data store
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:#"Category"];
self.categories = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
NSLog(#"name: %#",self.categories);
[self.tableView reloadData];
// IMSAppDelegate * appDelegate = [UIApplication sharedApplication].delegate;
// self.managedObjectContext = appDelegate.managedObjectContext;
//
// // Fetching Records and saving it in "fetchedRecordsArray" object
// self.categories = [[appDelegate getallcategories]mutableCopy];
// NSLog(#"name: %#",self.categories);
// [self.tableView reloadData];
}
- (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 [self.getallcategories count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// UITableViewCell *cell = [UITableView dequeueReusableCellWithIdentifier:#"Cell"];
//add this
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
Category * category = [self.getallcategories objectAtIndex:indexPath.row];
cell.textLabel.text = [category name];
return cell;
}
There are two problems.
1) You are accessing name which is a property on an NSEntityDescription object. Maybe you are just doing this temporarily, but if you created a name property in your Category entity, then you'll have to change that property name to something else (e.g. categoryName or itemName) since this is already reserved.
2) When you put an NSLog in the cellForRowAtIndexPath method to show the category, it returned an array of categories, so you're obviously not accessing the data source correctly.
The problem is that you're using [self.getallcategories objectAtIndex:indexPath.row]; instead of accessing the original array that you created in the viewDidLoad method which should instead be:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Access the proper array here that you loaded in viewDidLoad
return [self.categories count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
// Access the proper array here that you loaded in viewDidLoad
Category * category = [self.categories objectAtIndex:indexPath.row];
cell.textLabel.text = [category name];
return cell;
}
As I mentioned, you should be using NSFetchedResultsController instead of executing a fetch like that for a tableview. NSFetchedResultsController is optimized for acting as the datasource for table views since it only fetches objects that are showing on screen.
Also, you are using the old way of instantiating a new table view cell. You should be using prototype cells (set the cell identifier in Interface Builder) and then your code would look like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Make sure your prototype cell in Interface Builder has an identifier called "Cell"
UITableViewCell *cell = [UITableView dequeueReusableCellWithIdentifier:#"Cell"];
// Access the proper array here that you loaded in viewDidLoad
Category *category = [self.categories objectAtIndex:indexPath.row];
// Assuming the Category entity has somePropertyNameHere method
cell.textLabel.text = category.somePropertyNameHere;
return cell;
}
please check your delegate and data source connection. set your table view delegate and data source.
#interface myViewController : UIViewController {...} set it. and refer table view tutorial
Related
A lot of the methods have deprecated in iOS 7 in order to set font, textLabel, and color for UITableView cells. I'm also just having a difficult time populating the view with these values. Here's a snippet of my code:
- (void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
NSArray* jobs = [json objectForKey:#"results"];
for(NSDictionary *jobsInfo in jobs) {
JobInfo *jobby = [[JobInfo alloc] init];
jobby.city = jobsInfo[#"city"];
jobby.company = jobsInfo[#"company"];
jobby.url = jobsInfo[#"url"];
jobby.title = jobsInfo[#"jobtitle"];
jobby.snippet = jobsInfo[#"snippet"];
jobby.state = jobsInfo[#"state"];
jobby.time = jobsInfo[#"date"];
jobsArray = [jobsInfo objectForKey:#"results"];
}
}
I am looping through an array of dictionaries from a GET request and parsed. I am now attempting to fill my UITableView with the following code:
-
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [jobsArray 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];
}
NSDictionary *jobsDic = [jobsArray objectAtIndex:indexPath.row];
[cell.textLabel setText:[jobsDic objectForKey:#"jobtitle"]];
return cell;
}
Also, I have declared this is in my .h file:
NSArray *jobsDic;
Any ideas on what I'm doing wrong? Is this an iOS 7 problem?
It seems that you reinitialize jobsarray at the end of the forin loop.
You didn't mean ?
NSArray* jobs = [json objectForKey:#"results"];
NSMutableArray *jobsTemp = [[NSMutableArray alloc] initWithCapacity:jobs.count];
for(NSDictionary *jobsInfo in jobs) {
JobInfo *jobby = [[JobInfo alloc] init];
jobby.city = jobsInfo[#"city"];
jobby.company = jobsInfo[#"company"];
jobby.url = jobsInfo[#"url"];
jobby.title = jobsInfo[#"jobtitle"];
jobby.snippet = jobsInfo[#"snippet"];
jobby.state = jobsInfo[#"state"];
jobby.time = jobsInfo[#"date"];
[jobsTemp addObject:jobby];
}
self.jobsArray = jobsTemp; //set #property (nonatomic, copy) NSArray *jobsArray; in the .h
[self.tableView reloadData]; //optional only if the data is loaded after the view
In the cell for row method :
- (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];
}
JobInfo *job = self.jobsArray[indexPath.row];
cell.textLabel.text = job.title;
return cell;
}
And don't forget :
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.jobsArray.count;
}
Update - an user suggested an edit :
It's true that count isn't a NSArray property. But as Objective-C lets us use a shortcut notation for calling method with a dot, this code works. You have to know limitation of this : if you use a NSArray subclass with a count property with a custom getter this could have side effects #property (nonatomic, strong, getter=myCustomCount) NSUInteger count. As I think code readability is to me one of most important things I prefer to use dot notation. I think Apple SHOULD implement count as readonly property so I use it this way (but it's my point of view). So for those which don't agree with me just read return [self.jobsArray count]; in the tableView:numberOfRowsInSection: method.
Change the declaration of jobsArray from NSArray to NSMutableArray.
Add an initialization at the beginning point of fetchedData method like follows.
if(!jobsArray) {
jobsArray = [NSMutableArray array];
}
else {
[jobsArray removeAllObjects];
}
Remove the following line.
jobsArray = [jobsInfo objectForKey:#"results"];
Instead of that, add the initialized object to the array at the end of for loop.
[jobsArray addObject:jobby];
Add a [tableView reloadData]; at the end of your *-(void)fetchedData:(NSData )responseData; method implementation.
Initially when you are loading the view, tableView will get populated. After you received the data, tableView will not be known that it is received.
Everything else seems good. Hope rest will work fine.
I'm trying to get a tableview starting from a string in this format "name1,link1,name2,link2..."
So what i'm actually doing is this:
-Get the string and put links and names into an array, then split the array into two lesser ones by the position of the objects
- (void)viewDidLoad
{
NSString *dataString = #"a,www.google.it,b,www.apple.it,c,www.youtube.it";
dataArray = [dataString componentsSeparatedByString:#","];
for (int i=0; i<[dataArray count]; i++) {
if (i%2==0) {
[dataArrayName addObject:[dataArray objectAtIndex:i]];
}
else {
[dataArrayLink addObject:[dataArray objectAtIndex:i]];
}
}
[super viewDidLoad];
}
-Set up the table view
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return [dataArrayName count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"myCell";
// Try to retrieve from the table view a now-unused cell with the given identifier.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
// If no cell is available, create a new one using the given identifier.
if (cell == nil) {
// Use the default cell style.
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"myCell"];
}
// Set up the cell.
NSString *cellName = [dataArrayName objectAtIndex:indexPath.row];
cell.textLabel.text = cellName;
return cell;
}
But when i run the application the view with the tableview is clear (there are empty rows)
What's the problem?
Have you initialized dataArrayName and dataArrayLink (ie, at some point, before you started adding objects to it, did you say dataArrayName = [[NSMutableArray alloc] init] or an equivalent?
I'm creating a chat application. I have 2 methods in my view controller one for sending and one for receiving the messages. In the sending method i create a NSMutableDictionary with two objects ..
NSMutableDictionary *msgFilter = [[NSMutableDictionary alloc] init];
[msgFilter setObject:messageStr forKey:#"msg"];
[msgFilter setObject:#"you" forKey:#"sender"];
[messages addObject:msgFilter];
"messages" is my main NSMutableArray for holding all the messages, whose property is set and synthesized and allocated. When i send the message it is properly added into the NSMutableArray and the UITableView is updated showing me the values in the cell.
I have a method in my appDelegate to check for messages received and use the same procedure to parse the data and store it in an NSMutableDictionary. This dictionary is then passed to the viewcontroller and added into the same NSMutableArray(messages) and i then call [self.chattable reloadData]. But this doesn't do anything. When i nsloged the NSMutableArray it only had the received message not the whole data(send + received).
Why is it not adding the received messages into the same array and why is it not refreshing my table. I've been trying to get it to work for days now...Plz help..
//Recives message section
NSMutableDictionary *msgFilter = [myDelegate msgFilter];
[messages addObject:msgFilter];
[self.tView reloadData];
//Tableview section
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [messages count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSDictionary *s = (NSDictionary *) [messages objectAtIndex:indexPath.row];
NSString *sender = [s objectForKey:#"sender"];
NSString *message = [s objectForKey:#"msg"];
if ([sender isEqualToString:#"you"])
{
cell.detailTextLabel.text = [NSString stringWithFormat:#"TX: %at", message];
}
else
{
cell.detailTextLabel.text = [NSString stringWithFormat:#"RX: %at", message];
}
return cell;
}
Declare messages array in Application Delegate. so it will be shared array. so might be your problem get solved. because it is shared array. so you can add Dictionary in messages array from any where, no need to pass dictionary between diff UIView.
I'm a beginner i have some problem with NSArray.
I created an array and add an new object(dataclass) to the locationArray
dataclass.m
#synthesize UniqueID,name, address, latitude, longitude, type;
+ (id)LocationWithID:(NSString *)UniqueID name:(NSString *)name address:(NSString *)address latitude:(NSString *)latitude longitude:(NSString *)longitude type:(NSString *)type
{
agilentLocation *newLocation = [[self alloc] init];
newLocation.UniqueID = UniqueID;
newLocation.name = name;
newLocation.address = address;
newLocation.latitude = latitude;
newLocation.longitude = longitude;
newLocation.type = type;
return newLocation;
}
Table.m
NSMutableArray *LocationArray;
[LocationArray addObject:[dataclass LocationWithID:UniqueID name:name address:address latitude:latitude longitude:longitude type:type]];
I would like to ask how could I call the value under my dataclass. For example I need to call the name under the dataclass for my table title.
Normally when i'm building a table i'll use
cell.textLabel.text = [LocationArray objectAtIndex:indexpath.row];
but in this case there is only one object which is "dataclass" but it has several objects under it.
how should i display the name on the table.
The code below is used to print out values for the object(dataclass)
for (dataclass *theLocation in LocationArray) {
NSLog(#"Name: %# Address: %# Latitude: %g Longitude: %g Type :%#", theLocation.name, theLocation.address ,[theLocation.latitude doubleValue], [theLocation.longitude doubleValue], theLocation.type);
}
Screenshot
// First Return The Number of Sections.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)iTableView {
NSInteger aSectionCount = 0;
return aSectionCount;
}
// Now return the No Of Rows. In your case the total count of DataClass in LocationArray
- (NSInteger)tableView:(UITableView *)iTableView numberOfRowsInSection:(NSInteger)iSection {
NSInteger aRowCount = 0;
aRowCount = [LocationArray count];
return aRowCount;
}
// Now for the respective Row, Pick the DataClass from Location Array and display the name.
- (UITableViewCell *)tableView:(UITableView *)iTableView cellForRowAtIndexPath:(NSIndexPath *)iIndexPath {
NSInteger theRow = iIndexPath.row;
NSInteger theSection = iIndexPath.section;
NSString *aCellIdentifier = [NSString stringWithFormat:#"Cell-%d-%d-%d", theSection, theRow, iTableView.tag];
UITableViewCell *aCell = (UITableViewCell *)[iTableView dequeueReusableCellWithIdentifier:aCellIdentifier];
if (aCell == nil) {
aCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:aCellIdentifier] autorelease];
}
dataclass *theLocation = [LocationArray objectAtIndex:theRow];
aCell.TitleLabel.Text = theLocation.name;
return aCell;
}
Please Note that the LocationArray should be a class variable. Public or Private based on your convenience.
Hope this is what you are looking for...
I am trying to access individual elements of my array. This is an example of the contents of the array i am trying to access.
<City: 0x4b77fd0> (entity: Spot; id: 0x4b7e580 <x-coredata://D902D50B-C945-42E2-8F71-EDB62222C0A7/Spot/p5> ; data: {
CityToProvince = 0x4b7dbd0 <x-coredata://D902D50B-C945-42E2-8F71-EDB62222C0A7/County/p15>;
Description = "Friend";
Email = "bla#bla.com";
Age = 21;
Name = "Adam";
Phone = "+44175240";
}),
The elements i am trying to access are Name, Phone, etc ...
How would i go about doing this?
UPDATE:
OK, understanding that I am looking at core data now, how would I go about removing an object from being displayed in my table view?
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSManagedObject *object = (NSManagedObject *)[entityArray objectAtIndex:indexPath.row];
NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
return cell;
}
That doesn't look like an array. That looks like a Core Data entity. You may have an array of them, but it appears as though you're trying to access the members of the entity itself. To do so, you can use the NSManagedObject method -(id) valueForKey:.
NSManagedObject *entity = /* ... retrieve entity from Core Data ... */;
NSString *name = [entity valueForKey:#"Name"];