objective-c. TableView cells have the same address - ios

every 12 cells in my tableview have the same address, this results in a problem: when i address to one cell, all cells with that address get called
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = #"datacell";
DataCell *cell = (DataCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell==nil) {
cell= [[DataCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
if (indexPath.row==1) {
cell.backgroundColor= [UIColor redColor];
}
return cell;
for example here, although i set red color to only one cell, every 13-th cell gets red background. So i have 4 cells with red background. I have no idea what is going on :#:#

Table view cells are reused when you scroll. For that reason, you have to
always set the properties of the cell, e.g.:
if (indexPath.row==1) {
cell.backgroundColor= [UIColor redColor];
} else {
cell.backgroundColor= [UIColor clearColor];
}

Related

Cell text colour

I am somehow having problems with something as simple as setting text colour with
-(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:CellIdentifier];
cell.textLabel.textColor=[UIColor whiteColor];
cell.detailTextLabel.textColor=[UIColor lightTextColor];
cell.backgroundColor=[UIColor clearColor];
}
NSArray *array=[[NSArray alloc]initWithArray:messagingArrayToBeDisplayedInTableWhenCalloutTapped[indexPath.row]];
cell.textLabel.textColor=[UIColor whiteColor];
cell.detailTextLabel.textColor=[UIColor lightTextColor];
cell.backgroundColor=[UIColor clearColor];
cell.textLabel.text=[array objectAtIndex:0];
cell.detailTextLabel.text=[array objectAtIndex:1];
return cell;
}
The background colour works, but setting text colour to whiteColor has no effect so both the title and subtitle are a default grey colour. I have tried changing colours in my prototype cell, but that also did not work. Is anyone able to suggest a solution? Thanks in advance :)
dequeueReusableCellWithIdentifier:forIndexPath: always returns a valid cell, there fore cell is never nil and that code is never executed.
So remove that condition and see if it works.

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.

UITableView - Change cell color on tap

I've read through quite a few posts and none of them answer this question. I've created a tableview that populates a list of player names. When the user taps on a name I want the background color of the cell to change to either red, yellow, or green depending on where they are at in the cycle. I am not trying to change the selectionStyle color. The changing of the background color will indicate how difficult of questions each player is going to receive in the game. This it what I've been trying so far. When I debug I can see that didSelectRowAtIndexPath is being hit, but nothing changes. Any Ideas?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Player Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.backgroundColor = [UIColor greenColor];
UILabel *playerName = (UILabel *)[cell viewWithTag:PLAYER_NAME];
playerName.backgroundColor = [UIColor redColor];
}
Try:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.backgroundColor = [UIColor greenColor];
UILabel *playerName = (UILabel *)[cell viewWithTag:PLAYER_NAME];
playerName.backgroundColor = [UIColor redColor];
}
This should work because you are only going to be asking for a visible cell. The table will return the already visible cell.
You really also need to update some state variable in your data source so if this cell scrolls off and then back into view, you will know to draw the cell again with the proper color.

Single UITableViewCell with AccessoryView

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.

How can I stop my table view cells loading their subviews multiple of times in iOS?

I am using FontLabel in the view cells of the table.
- (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];
}
FontLabel *author_name = [[FontLabel alloc] initWithFrame:CGRectMake(58, 10, 217, 16) fontName:#"MyriadPro-Bold" pointSize:12.0f];
author_name.numberOfLines = 1;
author_name.text = [dummyArray objectAtIndex:indexPath.row];
author_name.textColor = [UIColor colorWithRed:0.580 green:0.776 blue:0.329 alpha:1.000];
author_name.backgroundColor = [UIColor clearColor];
[cell addSubview:author_name];
return cell;
}
But the label is loaded multiple times. I can see that it is getting bolder and bolder. How can I fix it?
When you call addSubview, it's not removing the old view. remember that cells are reused so the view you added in the last iteration is still there.
You should instead subclass UITableViewCell so that you can add a UILabel to it.
Edit:
Or you can do
author_name.tag = 10;
and then
FontLabel *author_name = (FontLabel *)[cell.contentView viewWithTag:10];
when you want to retrieve it.

Resources