I have a problem and cellForRowAtIndexPath is not being called. I checked that both the no of row and sections are not zero. Thanks in advance for any advise.
- (void)viewDidLoad
{
[super viewDidLoad];
MakeupTable=[[UITableView alloc]init];
MakeupTable.dataSource=self;
MakeupTable.delegate=self;
[MakeupTable reloadData];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section==0) {
return 4;
}else{
return 1;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
return cell;
}
Make sure this class is set as Table view delegates:
#interface YourViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
Related
I am pretty new to Objective-C and I am stuck in this part. I am trying to display the controls the textfield, label, Gradient views, and the Transfer button which are inside the content view of the cell, but it does not appear when I test it out. It only shows "1. Data to operate" twice but no content.. Can someone please help me out with this? I would appreciate it.
Below is how my ViewController looks like:
#implementation CNCTransfersOwnViewController{
}
//#synthesize delegate;
+ (id)instanceFromStoryboard {
return [self instanceFromStoryboardNamed:#"Transfer"];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self setUpUI];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)setUpUI {
self.navigationBar.topItem.title = #"GO TO SOMEWHERE ELSE";
}
- (NSString *)identifierForCellAtIndexPath:(NSIndexPath *)indexPath {
NSString *identifier = #"";
identifier = #"OPERATION";
return identifier;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(self.tableView == tableView){
return 2;
}else{
return 0;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(self.tableView == tableView){
NSString *identifier = [self identifierForCellAtIndexPath:indexPath];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
return nil;
}
You need to set the text you want to display in the cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(self.tableView == tableView){
NSString *identifier = [self identifierForCellAtIndexPath:indexPath];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.text = #"The text you want to display";
return cell;
}
return nil;
}
I have a tableview which have 4 row. then i want to insert a collectionview into tableview cell and each collectionview have different cell. But i can't do that. can any one help me out or any suggesition?
You can give different CellIdentifiers for every TableViewCell and createa different rows in InterfaceBuilder or inside
cellForRowAtIndexPath
Dont forget to provide dataSource and delegate for every collectionView that you will create for TableViewCell
Pseudocode:
YN Means YourNamespace
YNDataSourceDelegate1* data1;
YNDataSourceDelegate2* data2;
YNDataSourceDelegate3* data3;
YNDataSourceDelegate4* data4;
const NSInteger COLLECTION_VIEW_TAG = 1001;
NSArray* providers=#[data1,data2,data3,data4];
NSArray* rowIds=#[#"row1",#"row2",#"row3",#"row4"];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:rowIds[indexPath.row]];
UICollectionView * collectionView = (UICollectionView*)[cell viewWithTag:COLLECTION_VIEW_TAG];
collectionView.dataSource = providers[indexPath.row];
return cell;
}
This code will help you
yourViewController.h
#interface testingvc : UIViewController
<
UITableViewDelegate,
UITableViewDataSource,
UICollectionViewDelegate,
UICollectionViewDataSource
>
{
IBOutlet UITableView *tbleeView;
}
yourViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
tbleeView.delegate=self;
tbleeView.dataSource=self;
tbleeView.pagingEnabled=YES;
}
yourTableViewCell.h(UITableViewCell)
#property(strong,nonatomic)IBOutlet UICollectionView *clview;
yourViewController.m
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Incomplete implementation, return the number of sections
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete implementation, return the number of rows
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *identifierDriver = #"abcdefgh";
yourTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierDriver forIndexPath:indexPath];
//testing table cell is a UITableViewCell
if (cell == nil)
{
cell = [[testingTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierDriver];
}
cell.backgroundColor=[UIColor yellowColor];
//#property(strong,nonatomic)IBOutlet UICollectionView *clview;-this method is implement in UITableViewCell
// call collection view delegate method in table view cell.
cell.clview.delegate=self;
cell.clview.dataSource=self;
cell.clview.parentIndexpath = indexPath;
cell.clview.backgroundColor=[UIColor magentaColor];
cell.clview.pagingEnabled=YES;//optional
return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
NSArray *collectionViewArray = [_arrayProducts [[(IndexedCollectionView *)collectionView parentIndexpath].row]objectForKey:#"inventory"];//arrProducts is an array where i get all the data from webservice
return [collectionViewArray count];
}
- (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point
{
return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
yourColloectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"abcdef" forIndexPath:indexPath];
NSArray *Products = [_arrayProducts[[(yourCollectionView *)collectionView parentIndexpath].row]objectForKey:#"inventory"];//arrProducts is an array where i get all the data from webservice
NSLog(#"object ibde xis ----%ld",[Products count]);
if (indexPath.row>[Products count]-1 )
{
NSLog(#"index not found");
}
else
{
NSDictionary *dictdata = [Products objectAtIndex:indexPath.row];
NSLog(#"dictdata is =====%#",dictdata);
//do your code here
}
if (indexPath.row % 2)
{
cell.backgroundColor=[UIColor brownColor];;
}
else
cell.backgroundColor=[UIColor redColor];;
return cell;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 0;
}
- (void)collectionView:(IndexedCollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger tableindex = collectionView.parentIndexpath.row;
NSLog(#"collection path row is ----%ld",tableindex);
NSLog(#"collection path row is ----%ld",indexPath.row);
//you can get all that index path number . Enjoy :)
}
I am facing new problem.In my app, i am getting data by using NSXmlParser.
After parsing data , i am creating UITableview manually.
Here is my code,
-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
parser = [[NSXMLParser alloc]initWithData:self.cData];
parser.delegate = self ;
[parser parse];
tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, 320, 460) style:UITableViewStylePlain];
tableView.dataSource = self;
tableView.delegate = self;
[self.view addSubview:tableView];
}
This is my delegates methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return eventName.count;
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
-(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]autorelease];
}
for (int i=0; i < eventName.count; i++)
{
cell.textLabel.text = [eventName objectAtIndex:i];
NSLog(#"%#",cell.textLabel.text);
}
return cell;
}
Instead of checking i in the for loop, remove the for loop and look for indexPath.row in-
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
...
cell.textLabel.text = [eventName objectAtIndex:indexPath.row];
...
}
It will be called as many times as the value returned by-
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{}
I have a very simple tableview in a xib file with it's delegate and datasource hooked up to TestVC. TestVC is simple:
#import "TestVC.h"
#implementation TestVC
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *IDEN = #"IDEN";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:IDEN];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:IDEN];
}
cell.textLabel.text = #"test";
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (void)tableView:(UITableView *)_tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"called");
[_tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#end
When I click on a cell, it doesn't get deselected. I even put a log statement there to see if didDeselectRowAtIndexPath method is being called and it is.
What am i doing wrong?
You are using the wrong method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
is the one you need
I'm having trouble adding cells with different content to my app. I know (or think) this needs to be done using an array, but I'm new to xcode and objective-c so I'm not exactly sure how to do this. Any help is much appreciated. Thanks.
Here is a copy of my current code:
#import "RootViewController.h"
#import "CustomCell.h"
#implementation RootViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.imageViews.image = [UIImage imageNamed:#"captainaq_4.jpg"];
cell.firstLabel.text = #"name ";
cell.secondLabel.text = #"second name";
cell.thirdLabel.text = #"third name";
cell.thirdLabel.textAlignment = UITextAlignmentCenter;
// Configure the cell.
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// Delete the row from the data source.
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert)
{
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}
- (void)dealloc
{
[super dealloc];
}
#end
First, note that cellForRowAtIndexPath is being called multiple times-- once for each cell. I think your best option would be to declare an array, and populate it with the objects you want displayed, as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *days[] = {#"Mon", #"Tues", #"Wed", #"Thurs", #"Fri", #"Sat", #"Sun"};
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[[cell textLabel] setText:days[indexPath.row]];
return cell;
}
Or populate the array somewhere else, if you need to manipulate the objects within it. In that case (assuming your array is populated with strings), your call to setText would look like:
[[cell textLabel] setText:[yourArray objectAtIndex:indexPath.row]];
It also appears you want to have a header with an image in it. In that case, use:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//create your UIImageView and add your image
return yourImageView;
}
ok... as far as I have seen , I dont think the following is valid.
cell.imageViews.image = [UIImage imageNamed:#"captainaq_4.jpg"];
cell.firstLabel.text = #"name ";
cell.secondLabel.text = #"second name";
cell.thirdLabel.text = #"third name";
cell.thirdLabel.textAlignment = UITextAlignmentCenter;
Correct me if I am wrong...
As far as I understood your question, what you got to do is that create labels and add them either programmatically or through Interface Builder ( whichever is easier for you) so that you can display whatever contents you want in the cell...
Here is an example of what you can do..
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; // adding indicator to the rows
}
// Configure the cell...
switch (indexPath.row) {
case 0:
cell.textLabel.text = #"John Doe";
cell.detailTextLabel.text = #"DEPT";
//cell.imageView.image = [UIImage imageNamed:#"meeting_color.png"];
break;
case 1:
cell.textLabel.text = #"Mary Smith";
cell.detailTextLabel.text = #"DEPT";
//cell.myImageView.image = [UIImage imageNamed:#"call_color.png"];
break;
case 2:
cell.textLabel.text = #"Bob Wong";
cell.detailTextLabel.text = #"DEPT";
//cell.myImageView.image = [UIImage imageNamed:#"calendar_color.png"];
break;
default:
break;
}
return cell;
}
Again that is a very simple way you can add content to a tableview...
You can create several different UITableViewCell or your own custom cell class and add it into array and display it later under:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
}
With this, you can have several distinct custom cell (What I mean distinct is totally different cell, such as first cell with image while the other doesn't have image).
While the other alternative is to do it inside the
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
as shown by using switch case. Either ways, it's about preference.
I suggest you to learn more about the basic use of classes, such as NSArray, NSMutableArray, and writing Objective-C language first. While it doesn't help much with learning developing app, it will speed up the learning curve during the real implementation of building program in Cocoa environment.