I have a custom tableViewCell with some subviews defined in a prototype cell in a storyboard. In the storyboard I set the Accessory to Disclosure Indicator, but when scrolling (when cells are reused) the indicator disappears.
I tried to set it in code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
It did not work as well. But I found a working solution, a very weird solution:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
cell.accessoryType = UITableViewCellAccessoryNone;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
I don't know why this can fix anything, but it works for me. Can anyone tell me why or if there is an other problem/fix?
UPDATE:
How i get the reusable cell:
ActivityWeekCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
I also tried:
ActivityWeekCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
if (cell == nil) {
cell = [[ActivityWeekCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
Try to use this variant:
ActivityWeekCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
if (cell == nil) {
cell = [[ActivityWeekCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
Related
I believe I'm doing this correctly but it doesn't seem to be working? The text for the cell is written indicating the if statement is correct and is running, However the cell accessory is not changing.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if (indexPath.section==0) {
if ([[tableContents objectAtIndex:indexPath.row] isEqual: #"New Time"]) {
cell.tintColor = [UIColor redColor];
cell.textLabel.text = [tableContents objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
else{
cell.textLabel.text = [tableContents objectAtIndex:indexPath.row];
}
}
else{
cell.textLabel.text = [tableContents objectAtIndex:indexPath.row];
}
return cell;
}
Thanks for any help in advance!
Okay so tried slimming the code down to try and see whats going on, now this crashes when the view is loaded, totally confused as i have another project where this is working perfectly!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"UserCells";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.textLabel.text = #"Hello";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
Setting the cell tint color in cellForRowAtIndexPath did work:
cell.tintColor = [UIColor redColor]; // whatever you want
I have a UITableView with sections inside of them. All works great, but I can't seem to figure out how to customize the very fist index path. For example, I know this is very easily achievable through a normal tableview like so:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
if (indexPath.row == 0)
{
cell.detailTextLabel.text = #"My Custom text";
NSLog(#"%#",pm1.customText1);
}
}
}
I have tried the above implementation and it does not seem to work for me. Is there a different approach for section headers to achieve the same result with a normal tableview? Do I need to inject a "fake" section in my dictionary?
-(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 (indexPath.row == 0) {
cell.detailTextLabel.text = #"My Custom text";
NSLog(#"%#",pm1.customText1);
}
return cell;
}
Try this
The way you have it now the first cell is customize only when cell is nil.
You should move it out of the if statement.
- (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];
}
//check for first cell
if (indexPath.row == 0) {
cell.detailTextLabel.text = #"My first Cell";
NSLog(#"%#",pm1.customText1);
}else{
cell.detailTextLabel.text = #"Other cells";
NSLog(#"other cells");
}
return cell;
}
I am writing the code to populate a UITableView from using a CoreData database wrapped in magical record. For some reason though the code docent seem to be working and my cell is considered nil. Why is this happening?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
PassWhizEntity *pazz = [allPasses objectAtIndex:indexPath.row];
cell.textLabel.text = pazz.destinationTeacherAttribute;
//cell.detailTextLabel.text = [self.dateFormatter stringFromDate:pazz.dateAttribute];
if (cell == nil)
{
NSLog(#"cell was nil");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
return cell;
}
it just prints cell was nil and loads a blank UITable. Any ideas?
Your code should be like
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSLog(#"cell was nil");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
PassWhizEntity *pazz = [allPasses objectAtIndex:indexPath.row];
cell.textLabel.text = pazz.destinationTeacherAttribute;
//cell.detailTextLabel.text = [self.dateFormatter stringFromDate:pazz.dateAttribute];
return cell;
}
You are trying to set textLabel of unallocated cell which have no effect on cell. Cell is returned nil because tableview don't have any cell that can be reused, so it is your responsibility to check for nil and make new Cell if needed.
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:
I have a table with cells where the subtitle only appears after something has been searched. This is the code for cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if(tableView == self.tableView)
{
// Configure the cell...
cell.textLabel.text = self.objects[indexPath.row];
cell.detailTextLabel.text = #"test2";
}
else{
// Configure the cell...
cell.textLabel.text = self.results[indexPath.row];
cell.detailTextLabel.text = #"test1";
}
return cell;
}
EDIT: ArtOfWarfare's answer was correct. it was a problem in the storyboard. See image below:
Your code suggests that you have two separate tables... I would suggest that your problem lies within the differences in how the tables have been configured, possibly in your Storyboard.
Do you, perchance, have one of them set with a prototype with an identifier of "Cell" but a style that isn't UITableViewCellStyleSubtitle? That would make it so that your cell creation method isn't called in one, but only in the other.