I do not have xib files or storyboards in my project. In this case how do I set the cell identifier? Below is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"test");
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [self.entries objectAtIndex:indexPath.row];
return cell;
}
Try to replace you code by this (using ARC) :
- (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];
cell.textLabel.text = [self.entries objectAtIndex:indexPath.row];
return cell;
}
++
If you're just using a UITableViewCell for your table view, you can register that class in viewDidLoad like this:
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"Cell"];
Use the same identifier in cellForRowAtIndexPath: that you use here.
Related
I am trying to create two tableviews into one single UIViewController, Here I have created two tableview with If conditional statement to operate separate process for separate tables. But the problem is I need to create separate cell.textLabel.text for separate tables. Whenever I clicked the cell I am getting first Tableview cell.textLabel.text value.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView==tableone)
{
static NSString *cellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.textLabel.text = [....]; // Here I am applying first table values
}
if (tableView==tabletwo) {
static NSString *cellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.textLabel.text = [....]; // here I am applying my values
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (tableView==tableone) {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
cellText = selectedCell.textLabel.text;
NSLog(#"%#",cellText);
}
if (tableView==tabletwo) {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
cellText = selectedCell.textLabel.text;
NSLog(#"%#",cellText);
}
}
You can give tag of the tableview in utility view.
after giving tag you can use tableview using given particular tag.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView.tag == 1)
{
static NSString *cellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.textLabel.text = [....];
}
if (tableView.tag ==2) {
static NSString *cellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.textLabel.text = [....];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (tableView.tag == 1) {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
cellText = selectedCell.textLabel.text;
NSLog(#"%#",cellText);
}
if (tableView.tag == 2) {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
cellText = selectedCell.textLabel.text;
NSLog(#"%#",cellText);
}
}
// I think this will help you..
I'm having an issue on iOS using a custom UITableView with a custom UITableViewCell. When I run the app on the Xcode Instruments I can see that there is an abandoned memory issue each time I select a cell from the custom TableView.
Please see the below a screen shot of the "Instruments".
http://i.stack.imgur.com/cKz22.png
Below is the code used to create a cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath2:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = [NSString stringWithFormat :#"cellID%i",indexPath.row];
customViewCell *cell = (customViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
NSInteger cellIndex = [indexPath row];
NSString* cellName = #"cell Name";
NSString* keyFile = #"0_48_video_1F0423D5-D2D7-466E-8D45-38C46E9B4425.mp4_1423522824.133377";
cell = [[customViewCell alloc] initWithStyle:UITableViewCellSelectionStyleNone reuseIdentifier:cellIdentifier withKeyFile:keyFile andRicochetName:ricochetName fromTableViewController:self;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
I would appreciate any expertise on helping identifying the memory issue. Please note that the App is built on ARC.
Thanks a lot
If you just have to pass keyFile name you use this way. Make sure you have linked keyFile in interface builder
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath2:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"cellID";
customViewCell *cell = (customViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
NSInteger cellIndex = [indexPath row];
NSString* cellName = #"cell Name";
NSString* keyFile = #"0_48_video_1F0423D5-D2D7-466E-8D45-38C46E9B4425.mp4_1423522824.133377";
cell.keyFile=keyFile;
return cell;
}
change the cell identifier to a static string.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath2:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"your tableview Cell Identifier";
customViewCell *cell = (customViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
NSString* cellName = #"cell Name";
NSString* keyFile = #"0_48_video_1F0423D5-D2D7-466E-8D45-38C46E9B4425.mp4_1423522824.133377";
cell = [[customViewCell alloc] initWithStyle:UITableViewCellSelectionStyleNone reuseIdentifier:cellIdentifier withKeyFile:keyFile andRicochetName:ricochetName fromTableViewController:self;
}
return cell;
}
I'm having a really annoying problem that I'm not sure WHY it's happening.
I have the Table view set, I have the correct Identifier in the Cell, but it still gives me "unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard" when I run it.
the code looks like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *event = [self.events objectAtIndex:indexPath.row];
cell.textLabel.text = [event objectForKey:#"name"];
return cell;
}
Any help would be great, cause I want to rip my hair. The table is connected to that class, and then Cell has that #"Cell" Identifier
Re-check all 4 steps
STEP 1:
Your cellForRowAtIndexPath should look like this
- (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:nil];
}
NSDictionary *event = [self.events objectAtIndex:indexPath.row];
cell.textLabel.text = [event objectForKey:#"name"];
return cell;
}
STEP 2: Your "ViewController.m" File should look like this
#import "ViewController.h"
#interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
#end
STEP 3:
PLEASE CHECK - Click on view controller move to Connection inspector - See if any other unwanted connections are present in this inspector.
STEP 4: Tableview cell attribute inspector should like this]
Right, you need to register a cell for reuse. This can be done programmatically after you've created your table view like this:
static NSString *cellIdentifier = #"Cell";
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
Or this can also be done in Interface Builder by selecting the cell and navigating to the attributes inspector and changing the cell's id.
Instead of:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Try:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if that does not work after
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
add:
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
You are not checking for cell is allocate or not.. You can try below code..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
// add a placeholder cell while waiting on table data
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"cell %d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = nil;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
NSDictionary *event = [self.events objectAtIndex:indexPath.row];
cell.textLabel.text = [event objectForKey:#"name"];
}
return cell;
}
In the Document Outline, verify that your prototype UITableViewCell is a child of the UITableView.
In the Attribute Inspector, verify that your Cell Identifier and Cell Style are properly set in storyboard:
In the Identity Inspector, verify that you have the class set to UITableViewCell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"GenusNameCell";
GenusNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[GenusNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"GenusNameCell"];
cell.GenusNameLabel.text = [genusName objectAtIndex:indexPath.row];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
return cell;
}
I have a tableview with an Array of objects but when I run it. Nothing shows up. Im fairly new to xcode but Im not sure what my mistake is in the code. Can some one help me out?
If the cellForRowAtIndexPath callback is never called, it could be:
you didn't have set the dataSource of your tableview;
you didn't implement the numberOfSectionsInTableView: and/or tableView:numberOfRowsInSection: callbacks.
And if you dequeue your cell, you need to set the text outside the if branch like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"GenusNameCell";
GenusNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[GenusNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"GenusNameCell"];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
cell.GenusNameLabel.text = [genusName objectAtIndex:indexPath.row];
return cell;
}
Change the code like this your code will run,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"GenusNameCell";
GenusNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[GenusNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"GenusNameCell"];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
cell.GenusNameLabel.text = [[genusName objectAtIndex:indexPath.row] geniusname];
return cell;
}
geniusname is the NSString you stored the name of the person.
I'm trying to customize a UITableViewCell, but for some reason it's showing up as blank. Is there something obvious I'm doing wrong? I've pulled the menuLabel from the storyboard as an outlet, so it's tied correctly, and the cell in the storyboard is linked to the class "MenuCell".
In my tableview controller
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"MenuCell";
[self.tableView registerClass:[MenuCell class] forCellReuseIdentifier:CellIdentifier];
MenuCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
NSLog(#"creating a new cell");
cell = [[MenuCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"MenuCell"];
}
cell.menuLabel.text = #"Hello";
return cell;
}
You can use this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"MenuCell";
MenuCell *cell = (MenuCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
UINib* customCellNib = [UINib nibWithNibName:#"MenuCell" bundle:nil];
// Register this nib file with cell identifier.
[tableView registerNib: customCellNib forCellReuseIdentifier:CellIdentifier];
cell = (MenuCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}
// Whatever you want
retrun cell;
}
Hope this will help. happy coding :P
Use this one ....
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"MenuCell";
MenuCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[MenuCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell = (MenuCell *)[[[NSBundle mainBundle] loadNibNamed:#"MenuCell" owner:self options:nil] objectAtIndex:0];
}
cell.menuLabel.text = #"Hello";
return cell;
}
I Hope it will be helpful.