I have two custom UITableViewCells I am trying to populate from an array that I am pulling from CoreData using MagicalRecord. The first three rows of data appear (the cells that are initially visible when the view loads) but when I scroll none of the cells below have any data populated into them. Also, when I scroll up the cells that initially displayed data are now blank. So it seems to be happening when any cell gets reused. I have been racking my brain and coming up empty on all my attempts to solve the problem. Here is mycellForRowAtIndexPath code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Explore *explore = [self.exploreCellData objectAtIndex:[indexPath row]];
if ([explore.belief_id isEqualToString:#"0"]) {
ExploreVoteCell *cell = (ExploreVoteCell *) [tableView dequeueReusableCellWithIdentifier:#"ExploreVote" forIndexPath:indexPath];
[cell.topicTitle setText:[explore topic_title]];
[cell.votesCount setText:[explore calc_totalVotes]];
[cell.timeDayCount setText:[explore day_difference]];
return cell;
} else {
ExploreBeliefCell *cell = (ExploreBeliefCell *) [tableView dequeueReusableCellWithIdentifier:#"ExploreBelief" forIndexPath:indexPath];
[cell.topicTitle setText:[explore topic_title]];
return cell;
}}
When I count the number of records in the array (for the number of rows) or NSLog some data elements when the table view loads the data is there. However, when I scroll the NSLog I set up inside the 'cellForRowAtIndexPath` method starts returning a null value once the scroll needs to start reusing cells.
I am new to using both MagicalRecords and AFNetworking so perhaps I am not be handling those correctly. Here is how I am pulling the data from CoreData into my NSMutableArray
- (void) fetchData {
self.exploreCellData = [NSMutableArray arrayWithArray:[Explore MR_findAllSortedBy:#"calc_totalVotes" ascending:YES]];}
Any help be greatly appreciated!
try using :
[tableView registerClass:[YOURCUSTOMCELL class] forCellReuseIdentifier:#"CUSTOMIDENTIFIER"];
in forexample viewDidLoad
and in cellForRowAtIndexPath:
-(UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ListCell * cell = [tableView dequeueReusableCellWithIdentifier:#"CUSTOMIDENTIFIER"];
return cell
}
Related
I'm attempting to pass a selected item in a tableview to be displayed in a UILabel on a second viewcontroller. Currently I can segue to the second viewcontroller but nothing displays. I've also attempted to create an instance of the selected item for it to be used as reference in a predicate. The segue I have is:
([segue.identifier isEqualToString:#"showResults"]); {
ResultsVC *rvc = (ResultsVC *) [segue destinationViewController];
NSIndexPath *indexPath = [self.searchTV indexPathForSelectedRow];
self.selectedItem = [_fetchedResultsController objectAtIndexPath:indexPath];
rvc.item = self.selectedItem;
rvc.managedObjectContext = self.managedObjectContext;
and in cell for row at index path:
Item *item = [_fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = item.itemName;
and in the second viewcontroller call the label to display the item:
self.itemNameLabel.text = _selectedItem.itemName;
I have tried self.selectedItem.itemName and other iterations with no luck.
What have I missed to get the UILabel to display data?
Thanks for the help!
Use NSUserDefaults to save the selected text and then retrieve it from the other view.
See this post to a similar question.
I was searching many hours and could not find any solution for, what I thought was simple, but as it seems it is not.
I have a tableView and attached a viewController as subclass of tableViewController.
Now I would just like to change the header color of the sections.
Due to the reason that this is not possible in storyboard, I would really like to know with which code I can make it happen.
this is work 100% and you can change your index or header text color
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
view.tintColor = [UIColor redColor];
// if you have index/header text in your tableview change your index text color
UITableViewHeaderFooterView *headerIndexText = (UITableViewHeaderFooterView *)view;
[headerIndexText.textLabel setTextColor:[UIColor blueColor]];
}
I have a UICollectionView that displays images. My images are size 114x133 but for some reason the actual UICollectionVewCell is bigger than this (I can see this because I set the cell background to red). Because of this the view can display only 2 images and there is a huge gap between the two. I am implementing the following
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
UIImage* img = [UIImage imageNamed:#"thumbnail_frame.png"];
return img.size;
}
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(1, 1, 1, 1);
}
I am creating the cell's contentview programmatically (i.e init imageView etc).
I face the same problem in a different view where I am creating the whole view programmatically. There I do:
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.minimumInteritemSpacing=10;
flowLayout.minimumLineSpacing= 30;
flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
if(collectionView != nil) {
[collectionView removeFromSuperview];
}
CGRect frm= CGRectMake(45, 80, 250, 230);
collectionView = [[UICollectionView alloc] initWithFrame:frm collectionViewLayout:flowLayout];//(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
collectionView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
collectionView.clipsToBounds = YES;
collectionView.layer.cornerRadius = 10.0;
collectionView.dataSource = self;
collectionView.delegate = self;
[collectionView registerClass:[YourPicCollectionViewCell class] forCellWithReuseIdentifier:#"YourPicCell"];
Again there is either a big gap between two images, or if I make the width smaller, there are 3 images but the right-most one is half-visible.
I tinkered with spacing but it doesn't seem to work. What could be the problem?
I think instead of resizing the cell.. you should try resizing the collection view. Make sure that the collection view has just enough width to hold 2 items in a row. This is because if the collection view has more width than the cells, then then will stick to the extreme right and left. If it's too short, then only one cell will be displayed at the centre (Assuming that you are trying to keep 2 cells in a row).
The reason that one of your cell was half visible is that the collection view had more width than its super view.
So doing the following would help you get what you want..
Frame your collection view such that its less than its super view and should be equal to (numberOfCells X width) + ((numberOfCells-1) X cellGap).
If it's a custom collection view cell try adding constraints to the imageView such that the image sticks to the cells edges.
I am using the following code:
CCMenuItemFont *controllerItem = [CCMenuItemFont itemFromString:#"Analog" target:self selector:#selector(controllerToggle)];
CCMenu *controllerTypeMenu = [CCMenu menuWithItems:controllerItem, nil];
[controllerTypeMenu alignItemsVerticallyWithPadding:30.0f];
controllerTypeMenu.position = CGPointMake(160.0f, 240.0f);
[self addChild:controllerTypeMenu z:0 tag:ControllerMenu];
}
-(void) controllerToggle
{
CCMenuItemFont *controllerItem = [self getChildByTag:888];
NSString * text = [NSString stringWithFormat: #"switching.. %f", CCRANDOM_0_1()];
[controllerItem setString:text];
}
In controllerToggle I would like to access to controllerItem and change the String to another value. Is this possible? I checked and CCMenu adds as child the CCMenuItems based the array order. But that's not an elegant solution. For the same reason I cannot add the CCMenuItem to the Scene as it will give me "child already added error". So I feel I should write my own "switch" button but I wonder if there is already something out there..
Any help?? Thanks!
you can declare needed menu item as class member. In this way you will be able to get access to it whenever you want
There is already CCMenuItemToogle which is used to switching the title or image of any menuitem on click event.Instead of taking CCMenuItem you can use CCMenuItemToggle..
Here is a sample code
CCmenuItemToggle *cam=[CCMenuItemToggle itemWithTarget:self selector:#selector(openCameraController) items:cameraOn,cameraOff, nil];
where cameraOn and cameraOff are two simple CCMenuItemImage which is added in CCMenuItemToggle and further this CCmenuItemToggle is added in a menu.
Now on every click of CCMenuItemToggle there will be a cameraOn and cameraOff state for same menuitem.
Hope this will help...
Hey guys, I appear to have run into a problem. I have an application (View-based) that has a UITableView displayed on the bottom half of the screen, with the selection of a cell then bringing up a custom cell taking up the bottom of the screen. I have a "more info" button on the bottom right of this cell, and and when it is selected, I wish for it to open a new NIB file, however the only thing I can manage to do is remove the tableView from the screen..I am not sure of what to use before the "addSubview" because it is not self.view which I thought it would be.
- (void)moreInfoButton:(id)selector{
NSLog(#"Button Pressed");
MoreInfo *mivc = [[MoreInfo alloc] initWithNibName:#"MoreInfo" bundle:nil];
[self.tableview removeFromSuperview];
//[self.view addSubview:(UIView *)mivc];
[self.navigationController pushViewController:mivc animated:YES];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"List of Events" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
[mivc release];
}
Also, the (UIView *) doesn't do the trick either. Any suggestions?
P.S. The UITableView does not use a navigationController at all, it is just the table, would this be the problem?
Also, what if I chose to just push a new view, and not use the navigationViewController for this view, is this possible?
I can't answer your navigation controller question without seeing more of your program. To answer your other question, without using a navigation controller you can just add a new view with:
newViewVC = [[NewViewVC alloc]initWithNibName:#"NewViewVC" bundle:nil];
[self.view addSubview:newViewVC.view];
Typically newViewVC is declared in your .h so you can release it later.
I have found an easy way: pushing it as a modal.
- (void)moreInfoButton:(id)selector{
NSLog(#"Button Pressed");
MoreInfo *mi= [[MoreInfo alloc] initWithNibName:#"MoreInfo" bundle:nil];
[self presentModalViewController:mi animated:YES];
}
All that is needed is a back button in the new nib file.