Custom init for TableView from storyboard - ios

Tried to search answer on google and stackoverflow but stuck.
I am using https://parse.com/docs/ios/api/Classes/PFQueryTableViewController.html
to fill up data with my objects.
Everything seems nice I got correct data for this table view
But I want to use my storyboard cell, but this seems I made wrong init.
- (id)initWithPhoto:(PFObject *)aPhoto {
// self = [super initWithStyle:UITableViewStylePlain];
//self = [super init];
self = [super initWithClassName:#"photo"];
if (self) {
// The className to query on
self.parseClassName = kPAPActivityClassKey;
// Whether the built-in pull-to-refresh is enabled
self.pullToRefreshEnabled = YES;
// Whether the built-in pagination is enabled
self.paginationEnabled = YES;
// The number of comments to show per page
// self.obcojectsPerPage = 30;
self.photo = aPhoto;
self.likersQueryInProgress = NO;
}
return self;
}
And here is code for cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
object:(PFObject *)object {
[self.tableView registerClass:[KNPhotoDetailsCommentCell class] forCellReuseIdentifier:#"cell"];
static NSString *cellIdent = #"cell";
KNPhotoDetailsCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdent];
PFFile *userPhoto = userObject[#"profilePictureSmall"];
[cell.userPhoto setFile:userPhoto];
[cell.userPhoto loadInBackground];
[cell.imageView setFile:userPhoto];
[cell.imageView loadInBackground];
[self configureBasicCell:cell atIndexPath:indexPath];
NSString *userComment = object[#"content"];
cell.userComment.text = userComment;
cell.textLabel.text = userComment;
return cell;
}
Please help me to achieve how to display my cell

I don't know any details about this particular Parse view controller, but if you define your cell as a prototype cell in your storyboard, you don't need to register anything in code. Just make sure you specify a reuse identifier for that cell in the storyboard and use the same reuse identifier inside cellForRowAtIndexPath.

I resolved it, first of all I fixed init for view controller, I called comment Vic from storyboard and then recreated my controller in storyboard after this I achieved custom pfquery cell

Related

UIImageView IBOutlet Properties for CollectionView Cells Not Loading

I have a UITableView and UICollectionView loading the same content, but, even though their protocol methods are defined almost identically, the collection view is giving me an empty view, whereas, the table view displays content. So that you understand the context of this question, I will cover what goes on before loading content on both views:
I get back an image from -imageWithData and store it in a dictionary
This dictionary is then accessed by the TableViewController class as well as the CollectionViewController class and implemented as such:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Configure the cell...
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"User ID" forIndexPath:indexPath];
//setting UI
NSString *milesFrom = self.streamItems[indexPath.row][#"MilesAway"];
cell.textLabel.text = [NSString stringWithFormat:#"%#, Distance Away: %#", self.streamItems[indexPath.row][#"UserName"], milesFrom];
UIImage *photo = [[UIImage alloc]init];
photo = self.streamItems[indexPath.row][#"ThumbnailPhoto"];
cell.imageView.image = photo;
return cell;
}
This part works just as I need it to, but the collection view does not:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = #"MyCell";
CellViewController *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImage *imageReturned = [[UIImage alloc]initWithCIImage: self.streamItems[indexPath.row][#"ThumbnailPhoto"]];
cell.imageOfCell.image = imageReturned;
[cell.imageOfCell sizeToFit];
return cell;
}
This CellViewController Class has the IBOutlet 'imageOfCell' property. This property references an Image View object that I dragged on top of the cell ("MyCell") that is inside of my collection view. The collection view, as I said, gives me a blank view with no content. In debugging, I saw that both the 'photo' instance and 'ImageReturned' instance had the same exact values. For this reason, I have no idea why the collection view does not load the images but the table view does. In case it has anything to do with the way I load the collection view, I have included my -viewDidLoad method definition:
#implementation StreamCollectionViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.collectionView registerClass: [CellViewController class]forCellWithReuseIdentifier:#"MyCell"];
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
[flow setScrollDirection:UICollectionViewScrollDirectionVertical];
[self.collectionView setCollectionViewLayout:flow];
//create object to deal with network requests
StreamController *networkRequester = [[StreamController alloc]init];
[networkRequester getFeedWithCompletion:^(NSMutableArray *items, NSError *error) {
if (!error) {
self.streamItems = items;
[self.collectionView reloadData];
}
else{
NSLog(#"Error getting streamItems: %#", error);
}
}];
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = NO;
// Register cell classes
// [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
// Do any additional setup after loading the view.
}
I am fairly new to using the collection view so I wouldn't be surprised if it's something relatively small that I am missing. Thanks in advance!
In delegate of uicollectionView cellforrowatindexpath
After
CellViewController *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
Add this
if(cell == nil)
cell = [[CellViewController alloc]init];

selectRowAtIndexPath from another UIVIewController not working

I am trying to call selectRowAtIndexPath on a UITableView that is a subview to the UIViewController I am calling it from.
I have set it up so that when you select a cell it goes grey and thats fine, however I am loading different data sets in and out of the UITableView and when ever a selection is made I am sending that selected NSIndexPath back to the UIViewController. Then when the view is next loaded with the correct data set for the NSIndexPath I call this method from my UIViewController.
if (codeIndexPath != nil) {
[filterViewController.tableView selectRowAtIndexPath:codeIndexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
}
Then in the UITableView class my cellForRowAtIndexPath and didSelectRowAtIndexPath look like this.
- (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];
}
// Configure the cell...
NSString *projectDescriptionString = [currentFilterMutableArray objectAtIndex:indexPath.row];
cell.textLabel.text = projectDescriptionString;
if (indexPath == currentlySelectedIndex) {
cell.highlighted = YES;
} else if (indexPath == currentlySelectedIndex) {
cell.highlighted = NO;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
// send this data back in the delegate so you can use it to show where the tick is again if you need too.
currentlySelectedIndex = indexPath;
[[self delegate] updateInstallTableWithFilter:[currentFilterMutableArray objectAtIndex:indexPath.row] FilterType:filterType InstallIndex:indexPath];
}
When It loads on the screen the correct cell will highlight for a second then go back to white.
Update
New if statment inside cellForRowAtIndexPath
if ([indexPath isEqual:currentlySelectedIndex]) {
cell.highlighted = YES;
} else if (![indexPath isEqual:currentlySelectedIndex]) {
cell.highlighted = NO;
}
I am still receiving the same error.
UITableViewController has a property called clearsSelectionOnViewWillAppear. From the doc:
When the table view is about to appear the first time it’s loaded, the
table-view controller reloads the table view’s data. It also clears
its selection (with or without animation, depending on the request)
every time the table view is displayed. The UITableViewController
class implements this in the superclass method viewWillAppear:. You
can disable this behavior by changing the value in the
clearsSelectionOnViewWillAppear property.
So in that table view controller subclass, in viewDidLoad...
- (void)viewDidLoad {
[super viewDidLoad];
self.clearsSelectionOnViewWillAppear = NO;
}

Custom table view cells show up blank

I have a regular view controller (not a table view controller) and on that view I have a tableview. My table view cells are custom and I just made them through storyboards (didn't do anything in the code) but when I run my application, the tableview is blank. Any ideas as to why tho is happening to me? I have looked at other things on here but all of these other scenarios have to do with the person using an NSArray to fill out the tableview in the code, but mine is custom so I am not doing that. Thanks for any help. And before you mark this duplicate, please actually read this.
my code is as follows:
#interface TTViewController ()
{
NSArray *messageComponents;
}
#end
#implementation TTViewController
#synthesize dateTimePicker, messageSetupTableView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title = #"Message Setup";
messageComponents = [[NSArray alloc] initWithObjects:#"Recipient",#"Message", #"Date",#"haha", nil];
messageSetupTableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
messageSetupTableView.alpha = 0.9;
}
#pragma mark -
#pragma mark Table view data source
-(NSInteger) numberOfSectionsInTableView:(UITableView *) tableView{
return [messageComponents count];
}
-(NSInteger)tableView:(UITableView *) tableVew numberOfRowsInSection:(NSInteger)section{
return 1;
}
// Customize the appearance of table view cells.
- (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];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
}
}
// Configure the cell.
cell.textLabel.text = [messageComponents objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[messageComponents objectAtIndex:indexPath.row]];
cell.backgroundColor = [UIColor redColor];
cell.textLabel.textColor = [UIColor whiteColor];
UIColor *selectedColor = [UIColor blueColor];
UIView *myBackgroundColor = [[UIView alloc] init];
[myBackgroundColor setBackgroundColor:selectedColor];
[cell setSelectedBackgroundView:myBackgroundColor];
return cell;
I want my table view to have a date picker in one section, a textview in the other, and a button and a few text fields in the other. Thanks
A couple of suggestions:
First make sure that UITableView's delegate and datasource are mapped or not either from IB or programmatically.
Put a break point on cellForRowAtIndexPath delegate function and see if it comes in this functions, and make sure you are using this function to create your custom cells.
Double check your have at least one row in your tableView using something similar:
- (NSInteger)numberOfRowsInSection:(NSInteger)section
{
return 5; // for test pass value 5 so tableView should aware to display 5 rows.
}
Hope it helps!

table view delegate methods not all called

In my code I'm using a tableview for which some of the methods are getting called, while others are not.
in initwithframe:
_table.delegate = self;
_table.dataSource = self;
this is called
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"TRAutocompleteCell";
id cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
cell = [_cellFactory createReusableCellWithIdentifier:identifier];
NSLog(#"got here 3");
NSAssert([cell isKindOfClass:[UITableViewCell class]], #"Cell must inherit from UITableViewCell");
NSAssert([cell conformsToProtocol:#protocol(TRAutocompletionCell)], #"Cell must conform TRAutocompletionCell");
UITableViewCell <TRAutocompletionCell> *completionCell = (UITableViewCell <TRAutocompletionCell> *) cell;
id suggestion = self.suggestions[(NSUInteger) indexPath.row];
NSAssert([suggestion conformsToProtocol:#protocol(TRSuggestionItem)], #"Suggestion item must conform TRSuggestionItem");
id <TRSuggestionItem> suggestionItem = (id <TRSuggestionItem>) suggestion;
[completionCell updateWith:suggestionItem];
return cell;
}
but this is not getting called in the same file.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(#"got here 5");
id suggestion = self.suggestions[(NSUInteger) indexPath.row];
NSLog(#"got here 4");
NSAssert([suggestion conformsToProtocol:#protocol(TRSuggestionItem)], #"Suggestion item must conform TRSuggestionItem");
self.selectedSuggestion = (id <TRSuggestionItem>) suggestion;
_queryTextField.text = self.selectedSuggestion.completionText;
[_queryTextField resignFirstResponder];
if (self.didAutocompleteWith)
self.didAutocompleteWith(self.selectedSuggestion);
}
The problem was not with the implementation, but my gesture recognition.
I have added a lister to my view to catch the tap event outside my text box, which was being called. I do not exactly know why these two are clashing, but when I removed it, it was working.
The tableview is for autocompletion in my textbox.
Did You do:
yourTableView.delegate = self;
yourTableView.dataSource = self;
While creating the tableView.
If it is .xib file , did you connect the delegate and dataSource?

iOS programmatic grouped table empty

I have a programmatically implemented tableView, in the grouped style.
All I am getting is the gray pinstripes when it should be populated. So it is loading, but not ... something...
What more is necessary?
If no more, then where else ought I look?
Also, how can I make the background color of the table the same as the cell's white color?
- (void)loadView {
[super loadView];
UITableView *view = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame style:UITableViewStyleGrouped];
[view setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
self.view = view;
[view reloadData];
}
Is viewDidLoad necessary?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
Thank you,
Morkrom
You have to provide your tableView with data.
For staters you'll need to define a dataSource. Its common to just use your viewController as the dataSource.
// in the .h find something similar and add <UITableViewDataSource>
#interface ViewController : UIViewController <UITableViewDataSource>
then when you make the tableView.
view.datasource = self;
Then you'll need to provide the data itself. Implement these methods:
#pragma mark - UITableView Datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
[cell.textLabel setText:#"A Cell"];
return cell;
}
Those methods will create 3 sections each with 3 rows. All the cells will just say A Cell. This is the basis for all tableViews. Just customize the data :)
You need to set dataSource and delegate properties for your table view so it will be able to pull data from them:
UITableView *view = ...
view.dataSource = self;
view.delegate = self;
have protocol in .h file and attach delegate and source with file owner

Resources