UITableViewController shows the same cell after updating to xcode 6 - ios8

My UITableViewController works not properly after updating xcode to version 6.
Im using custom cells in my tableview:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
BookDataTableViewCell * cell = (BookDataTableViewCell*) [tableView dequeueReusableCellWithIdentifier:#"BookDataTableViewCell"];
BookData * data = [_searchResult objectAtIndex:indexPath.row];
[cell setBookData:data];
NSLog(#"BookDataTableViewController: cell pointer = %p", cell);
return cell;
}
// BookDataTableViewCell.m
#interface BookDataTableViewCell()
#property (weak, nonatomic) IBOutlet UILabel * bookTitle;
#property (weak, nonatomic) IBOutlet UILabel * bookAuthor;
#end
#implementation BookDataTableViewCell
-(void)setBookData:(BookData *)bookData{
_bookData = bookData;
NSLog(#"BookDataTableViewCell: _bookTitle pointer = %p", _bookTitle);
NSLog(#"BookDataTableViewCell: bookTitle = %#", _bookData.title); // allways different!!!
[[self bookTitle] setText:_bookData.title];
[[self bookAuthor] setText:_bookData.author];
}
}
After updating xcode to version 6 all controller cells show the same book info using the last BookData in _searchResult list, although the data passed to BookDataTableViewCell are allways different.
Yesterday, befor update, the controller worked as expected...
Some idea what might have gone wrong?

Related

How to do searching in NSTableView with NSSearchField?

I have implemented a application in which I use NSTableview with the help of its data source and delegates I have not used NSArrayController nor I want to use it. My question is how can I bind NSSearchField with my NSTableView in this situation? I had seen a lot of answer using NSArrayController.
I do not want to convert implementation to NSArrayController as things are working good with NSMutableArray.
TableView is a display control and is not for filtering.
You should add 2 NSArray properties;
1) #property(nonatomic, strong) NSArray *allItems;
2) #property(nonatomic, strong) NSArray *filteredItems;
#import "ViewController.h"
#interface ViewController()<NSSearchFieldDelegate, NSTableViewDelegate, NSTableViewDataSource>
// Your NSSearchField
#property (weak) IBOutlet NSSearchField *searchField;
// Your NSTableView
#property (weak) IBOutlet NSTableView *tableView;
// In this array you will store all items
#property(nonatomic, strong) NSArray *allItems;
// In this array you will store only filtered items
#property(nonatomic, strong) NSArray *filteredItems;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.searchField.delegate = self;// You can set delegate from XIB/Storyboard
self.tableView.delegate = self;// You can set delegate from XIB/Storyboard
self.tableView.dataSource = self;// You can set dataSource from XIB/Storyboard
self.allItems = #[#"Test1", #"Demo filter", #"Test 2", #"Abracadabra"];
[self applyFilterWithString:#""];
}
- (void)controlTextDidChange:(NSNotification *)obj{
if (obj.object == self.searchField) {
[self applyFilterWithString:self.searchField.stringValue];
}
}
-(void)applyFilterWithString:(NSString*)filter {
if (filter.length>0) {
NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:#"self CONTAINS[cd] %#", filter];
self.filteredItems = [self.allItems filteredArrayUsingPredicate:filterPredicate];
}
else {
self.filteredItems = self.allItems.copy;
}
[self.tableView reloadData];
}
#pragma mark - ***** NSTableViewDataSource, NSTableViewDelegate *****
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return self.filteredItems.count;
}
// for the "Cell Based" TableView
- (nullable id)tableView:(NSTableView *)tableView objectValueForTableColumn:(nullable NSTableColumn *)tableColumn row:(NSInteger)row {
NSString *item = self.filteredItems[row];
return item;
}
#end

iOS 7: Cannot connect to IBOutlets, probably because I'm using UITableViewController

I cannot create any IBOutlets. I'm using a tableviewcontroller instead of a viewcontroller.
When I click on TableViewController, the class is UITableViewController and I can't change that.
Here's my code for ViewController.h:
// ViewController.h
// Tips4
//
// Created by Meghan on 1/20/14.
// Copyright (c) 2014 Meghan. All rights reserved.
//
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
#property (strong, nonatomic) IBOutlet UILabel *sliderDisplay;
#property (strong, nonatomic) IBOutlet UITextField *tempText;
#property (strong, nonatomic) IBOutlet UILabel *billTotal;
#property (nonatomic, strong) IBOutlet UISlider *slider;
- (IBAction)sliderValueChanged:(id)sender;
#property (nonatomic) float answer;
#property (nonatomic) float answerTwo;
#end
Here's my ViewController.m:
// ViewController.m
// Tips4
//
// Created by Meghan on 1/20/14.
// Copyright (c) 2014 Meghan. All rights reserved.
//
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *tipsTableIdentifier = #"TipsTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tipsTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tipsTableIdentifier];
}
return cell;
}
- (IBAction)sliderValueChanged:(id)sender
{
float theText = [_tempText.text floatValue];
_answer = (_slider.value * theText) / 100;
_answerTwo = _answer + theText;
_sliderDisplay.text = [NSString stringWithFormat:#"%1.2f", _answer];
_billTotal.text = [NSString stringWithFormat:#"%1.2f", _answerTwo];
}
- (void)viewDidLoad
{
[super viewDidLoad];
_tempText.keyboardType = UIKeyboardTypeDecimalPad;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
If you’re using a TableViewController, your class must inherit from a UITableViewController. Thats when it will show up in the Identity Inspector, which is where you change your class from UIViewController to your class. After that you should be able to connect IBOutlets.
To do that, just replace your current line
#interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
with
#interface ViewController : UITableViewController
Now, you will need to add the init method that calls the super, to the .m file.
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
I hope this will do it.
Also, sometimes, changes in the code don’t show up in the storyboard Identity Inspector. In that case, you can just quit the Xcode window and open the project again. That does it.
Alternatively, if you use a ViewController, your class can inherit from a UIViewController. Then you add a TableView to your View and add a UITableView instance to the controller file (create an IBOutlet). In this case, your .h file needs to add the UITableViewDelegate and UITableViewDataSource to populate the table and your .m file needs to implement the required methods (Xcode will warn you about this).

xCode 4.5 Navigation Controller and back button how to save values

In my code I created this
#import <UIKit/UIKit.h>
#interface ViewControllerTable : UIViewController <UITableViewDelegate, UITableViewDataSource>
#property (weak, nonatomic) IBOutlet UITableView *myTableView;
#property (strong, nonatomic) NSString *plcVar;
#end
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
listaPlacas *placas = [[listaPlacas alloc]init];
[self.navigationController pushViewController:placas animated:YES];
}
But in the other view:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
for (UITableViewCell *cell in [tableView visibleCells]) {
cell.accessoryType = UITableViewCellAccessoryNone;
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
NSString *placaVar = cell.textLabel.text;
ViewControllerTable *VCT = [[ViewControllerTable alloc]init];
VCT.plcVar = [[NSString alloc]init];
VCT.plcVar = placaVar;
}
When I click in back button the value in variable plcVar is empty.
I assume that "other view" is the viewController you push to from your first ViewController, and you are trying to set a property in the firstViewController from your otherViewController.
Then here is where you go wrong:
ViewControllerTable *VCT = [[ViewControllerTable alloc]init];
You are creating a new instance of VCT, when in fact you want a reference to the VCT that you pushed from.
You can get this via your navigation controller...
NSArray* viewControllers = [self.navigationController viewControllers];
ViewControllerTable* VCT = [viewControllers objectAtIndex:
[ viewControllers count]-2];

cell.detailTextLabel not working

I want to set up a textLabel and a detailTextLabel for my table. The textLabel is working properly. However, I couldnt get the detailTextLabel to display the text that I have set. Below is my code
- (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] autorelease];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = #"SomeString";
cell.detailTextLabel.text = #"hello";
return cell;
}
I have faced similar issue as like #user1829700.
My initial settings are
TableView --> Prototype Cell --> (Attribute Inspector) Style - set to
Custom (by design)
Changed Style to - Subtitle, it does the trick
Try this
Use appropriate UITableViewCellStyle with this (anything but UITableViewCellStyleDefault should thus work). The cell's style is specified when you initialize it.
In your custom table cell implementation try adding #synthesize detailTextLabel;
Header (.h):
#property (nonatomic, weak) IBOutlet UILabel *textLabel;
#property (nonatomic, weak) IBOutlet UILabel *detailTextLabel;
Implementation (.m):
#synthesize textLabel;
#synthesize detailTextLabel;

Collection View Cell Won't Allow Me to Select

This is probably a really easy fix; I have been reading countless forums and tutorials and can't seem to find answer.
In my app I have a popover collection view to select a specific option and return a value...
However it won't highlight or select? I can see the NS Log Outputs never show a selection or datapass.
Here is my code...
collection.h file
#interface collection : UICollectionViewController <UICollectionViewDataSource, UICollectionViewDelegate>
#property (nonatomic, retain) NSArray *counterImages;
#property (nonatomic, retain) NSArray *descriptions;
#property (nonatomic, weak) UILabel *graniteselect;
#property (nonatomic, retain) UIPopoverController* _collectionPopOver;
#property (nonatomic) BOOL clearsSelectionOnViewWillAppear; // defaults to YES, and if YES, any selection is cleared in viewWillAppear:
#property (nonatomic) BOOL allowsSelection;
#end
implementation file:
#pragma mark UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:
(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
return _descriptions.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCell *myCell = [collectionView
dequeueReusableCellWithReuseIdentifier:#"MyCell"
forIndexPath:indexPath];
UIImage *image;
int row = [indexPath row];
image = [UIImage imageNamed:_counterImages[row]];
myCell.imageView.image = image;
myCell.celltext.text= [_descriptions objectAtIndex:row];
return myCell;
}
- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
_graniteselect.text = [_descriptions objectAtIndex:row];
[self._collectionPopOver dismissPopoverAnimated:YES];
NSLog(#"Setting Value for Granite Select");
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath: (NSIndexPath *)indexPath {
// TODO: Deselect item
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
RoleDetailTVC *destination =
[segue destinationViewController];
destination.graniteselect = _graniteselect;
NSLog(#"Passing Data to RoleDetailTVC");
}
#end
There are a number of issues here. Firstly, I think you mixed up the delegate methods for selection. The collectionView:performAction:forItemAtIndexPath:withSender: method is only for special menu popovers and has nothing to do with selecting a cell per se. What you need to implement in your delegate is this:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Selected cell at index path %#", indexPath);
}
right now you're just implementing the deselect callback.
Beyond that, also make sure that your image view in the UICollectionViewCell has touches enabled, otherwise you might be blocking touch events to reach the cell and it won't fire any delegate calls. I hope that helps!

Resources