Single UITableViewCell with AccessoryView - ios

I'm trying to build an UITableView with some long texts inside each cell. Cells are without AccessoryView, except for one cell (the 8th one), that is a sort of button to open a detail view.
Consider this code:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGSize size = [[quotes objectAtIndex:indexPath.row] sizeWithFont:16 constrainedToSize:CGSizeMake(self.view.frame.size.width, CGFLOAT_MAX)];
return size.height+20;
}
- (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.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = [quotes objectAtIndex:indexPath.row];
if(indexPath.row==7){
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
}
return cell;
}
It works, but the problem is that when I scroll to the bottom of the Table (the 8th is also the last row) and then I go back to the upper side, another AccessoryView is added to a random point (more or less the 3rd cell, but I don't know if it is inside of it or is floating around randomly).
Is it something related to cell reusing by iOS? How can I avoid it?
Thanks in advance.

You have to explicitly set no disclosure button to every cell but the one that you wish to have disclosure. This way when the cell gets reused elsewhere its disclosure indicator is removed:
if(indexPath.row==7){
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
}else{
[cell setAccessoryType:UITableViewCellAccessoryNone];
}

The cell is being reused (as demonstrated by your call to -dequeueReusableCellWithIdentifer).
The answer is to set the cell to wanted defaults after it's been dequeued, or add an else clause to the if statement to handle it.
- (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.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = [quotes objectAtIndex:indexPath.row];
// Set to expected default
[cell setAccessoryType:UITableViewCellAccessoryNone];
if(indexPath.row==7){
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
}
return cell;
}

This is due to cell reuse as you surmise. You must explicitly set UITableViewCellAccessoryNone for cells at index paths other than 7.

Related

UITableViewCell accessoryType won't be visible

The cell's accessory type won't display. I have tried both cell.accessoryType and cell.accessoryView (with an image). The table view works fine aside from this. Any suggestions? Thanks
My code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = #"Hi";
cell.detailTextLabel.text = #"Hi again!";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
Make sure your table cell width fit into the screen. You are creating UITableView programmatically or it is in nib? If you are creating programmatically, check the frame size of table view also you can share your code here.
Choose accessory type from xib / storyboard of your table view cell.

UITableView disappearing content

As soon as the table view gets touched the cell titles (and on-tap actions) disappear. I only use standard table view cells and store the values in an array. After the values disappear the table stays scrollable. Any ideas?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.textLabel.text = [[systeme objectAtIndex:indexPath.row] description];
cell.backgroundColor = [UIColor clearColor];
[cell.textLabel setTextColor:[UIColor whiteColor]];
[cell.textLabel setTextAlignment:NSTextAlignmentCenter];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[[NSNotificationCenter defaultCenter] postNotificationName:#"choseSystem" object:[systeme objectAtIndex:indexPath.row]];
}
You should be sure that the reuse identifier is the same for all cells if you use only one type of cells. You should do something similar to the following in the portion of your code where to retrieve a reusable cell:
NSString *CellIdentifier = [NSString stringWithFormat:#"CellReuseIdentifier", (long)indexPath.section];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
And make you you set the #"CellReuseIdentifier" in your xib file or your storyboard.
If you would like to use multiple custom cells for a table view you should do something similar to what you're doing, but take into account that reuse identifiers need to be configured for every type of cells.
Hope this helps!
The table view was fine. I just added its view as a subview to another view without keeping reference to the actual UITableViewController. That was the problem.

UITableViewCell subtitle not appearing on load

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.

Static UITableViewCell backgroundView

In my project I have tableViews with static cells as well as tableViews with dynamic cells. In order to customized I've managed to get a gradient background on the cells (grouped sytle).
It works ok with dynamic TableViews as I set the background view in cellForRowAtIndex... according to the position of the row (Top, Bottom, Middle or single).
However, when I try to implement it on the static tableview cells, it doesn't work. I've tried to implement the cellForRowAtindex... but it crashes.
Does someone have an idea?
Update: the code for cellForRow..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UACellBackgroundView *bgw=[[UACellBackgroundView alloc]init];
if (indexPath.row==0) {
bgw.position = UACellBackgroundViewPositionTop;
cell.backgroundView=bgw;
}else if (indexPath.row==2){
bgw.position = UACellBackgroundViewPositionBottom;
cell.backgroundView=bgw;
}else {
bgw.position = UACellBackgroundViewPositionMiddle;
cell.backgroundView=bgw;
}
// cell.backgroundView=bgw;
return cell;
}
By the way, the Background view I got it from here: http://code.coneybeare.net/how-to-make-custom-drawn-gradient-backgrounds and here: http://pessoal.org/blog/2009/02/25/customizing-the-background-border-colors-of-a-uitableview/
if somebody is interested
It doesn't look like you are alloc the UITablViewCell, you need to alloc the cell.
For example:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// alloc the UITableViewCell
// remeber if you are not using ARC you need to autorelease this cell
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"Cell Name";
cell.detailTextLabel.text = #"Cell Detail";
return cell;
}
Add this statement:
if (cell == nil) {
// alloc the UITableViewCell
// remeber if you are not using ARC you need to autorelease this cell
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
If you have a UITableViewController subclass with a static table you should not try to dequeue cells.
Instead you should ask super for the cell. The superclass will get the cell from the storyboard and you can configure it.
Something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
UIView *selectedBackgroundView = [[UIView alloc] init];
cell.selectedBackgroundView = selectedBackgroundView;
cell.selectedBackgroundView.backgroundColor = [UIColor mb_tableViewSelectionColor];
return cell;
}
Works for all other attributes as well.

UITableViewCell initWithStyle:UITableViewCellStyleSubtitle is not working

I'm having an issue in trying to display info in a cell, one on the left and one on the right. I'm aware using initWithStyle with UITableViewCellStyleSubtitle. I use this but it doesn't seem to work.
Here is some sample code:
- (UITableViewCell *)tableView:(UITableView *)ltableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Account Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:Cellidentifier];
}
Accounts *account = [self.fetchedResultsController objectAtIndexPath];
cell.textLabel.text = account.name;
cell.detailTextLabel.text = #"Price";
return cell;
}
I can display cell.textLabel.text just fine, however I cannot get the simple "Price" to be displayed. I've tried different things, such as setting the font size of cell.detailTextLabel.
I've also tried UITableViewCellStyleValue1 as some had suggested in older posts.
Threw NSLog after setting to "Price", shows cell.detailTextLabel as null.
Not sure what I'm doing wrong.
Edit: I found this: cell.detailTextLabel.text is NULL
If I remove if (cell == nil) it works...
That check should be in place, so how do you make it work when using the different styles?
When using storyboards and prototype cells, a cell is always returned from the dequeue method (assuming a prototype with that identifier exists). This means you never get into the (cell == nil) block.
In your case the prototype cell is not defined in the storyboard with the subtitle style, so a subtitled cell is never used, and the detail text label does not exist. Change the prototype in the storyboard to have the subtitle style.
Remove all your code just once you try these lines only and check this will work or not.
-(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.textLabel.text=[Array objectAtIndex:indexPath.row];
cell.detailTextLabel.text=#"Price";
return cell;
}
I see the problem: in your method name, the UITableView variable is named ltableView, not tableView. Change it to tableView.
cell.detailTextLable.text should be cell.detailTextLabel.text. It looks like a simple mis-spelling of label.
All the answers mentioned here are really a workaround i.e. using storyboard.
Here is a way to do it only in code.
Basically instead of registering the identifier for the cell in viewDidLoad do it only once in cellForRowAtIndexPath: method. Also reset cell registered in viewDidLoad __sCellRegistered = 0;
static int _sCellRegistered = 0;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
if (__sCellRegistered == 0) {
__sCellRegistered = 1;
NSLog(#"register cell");
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:#"CellIdentifier"];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"CellIdentifier"];
};
if (!cell) {
NSLog(#"dequeue");
cell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifier" forIndexPath:indexPath];
}

Resources