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.
Related
I am trying to load my UITextView on UItableViewCell with data but unable to set text. UITextViewDelegate is also set and attached to view controller. The text string is not empty as I checked it using debugger.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CommentCellIdentifier];
//CommentCell is my custom cell with textView.
CommentCell *commentsCell = (CommentCell*)[tableView dequeueReusableCellWithIdentifier:CommentCellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CommentCellIdentifier];
}
//Setting the images for all buttons in service row.
[commentsCell.deletecomment setImage:deleteComment forState:UIControlStateNormal];
[commentsCell.editcomment setImage:editComment forState:UIControlStateNormal];
commentsCell.deletecomment.layer.borderColor = [UIColor blackColor].CGColor;
commentsCell.editcomment.layer.borderColor = [UIColor blackColor].CGColor;
NSInteger commentIndex = 2;
//CommentsArray is NSArray with data.
[commentCell.comments setText:[NSString stringWithFormat:#"%#",[[commentsArray objectAtIndex:indexPath.row] objectAtIndex:commentIndex]]];
return cell;
}
Try changing this line:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CommentCellIdentifier];
to this line:
CommentCell *cell = (CommentCell*)[tableView dequeueReusableCellWithIdentifier:CommentCellIdentifier];
I was using wrong cellname. Just replaced "commentCell" with "commentsCell" and everything starts working.
[commentsCell.comments setText:[NSString stringWithFormat:#"%#",[[commentsArray objectAtIndex:indexPath.row] objectAtIndex:commentIndex]]];`enter code here`
If you do not need scrolling the textview DISABLE SCROLLING in order for the text to appear.
I am not sure if this is caused by the fact that both the tableview and textview are descendants of UIScrollView, but it could be.
It helped me when I was experiencing the same issue
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.
I want to change the selected cell background color and keep the text on that cell. Then I did like this, it can change the cell color but the text on the cell will disappear.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MessageCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [self.receivedData objectAtIndex:indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"MessageCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UIView *bgColorView = [[UIView alloc]init];
bgColorView.backgroundColor = [UIColor colorWithRed:0.529 green:0.808 blue:0.922 alpha:0.5];
cell.selectedBackgroundView = bgColorView;
}
You are replacing the current view (the one that has got your text in it) with a new instance of UIView (with no text in it, but your new color). That is why your text is disappearing. You should change the background color property of selectedBackgroundview like so:
cell.selectedBackgroundView.backgroundColor = [UIColor colorWithRed:0.529 green:0.808 blue:0.922 alpha:0.5];
Although I think you can even ditch the selectedBackgroundView part and just do cell.backgroundColor = ... in didSelectRowAtIndexPath
I got a really weird problem with a custom UITableViewCell. Btw, i am using an UIViewController. So, i crafted the cell in Storyboard (like in the image bellow) and i set it's class to my custom UITableViewCell class. Then I created all the IBOutlets and IBActions in the custom cell class.
My cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
PostTableViewCell *cell = (PostTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[PostTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell setCellContentWithPost:[PostsArray objectAtIndex:indexPath.row]];
return cell;
}
My custom UITableViewCell class:
#import "PostTableViewCell.h"
#implementation PostTableViewCell
- (void)setCellContentWithPost:(SDPost*)post {
self.alpha = 0.f;
self.postTitleLabel.text = post.title;
[self.thumbnailImageView sd_setImageWithURL:[NSURL URLWithString:post.thumbnailURL] placeholderImage:nil options:SDWebImageHandleCookies];
[UIView animateWithDuration:0.35 animations:^{
self.alpha = 1.0f;
}];
}
-(void)awakeFromNib{
self.postTitleLabel.textColor = [UIColor whiteColor];
self.postTitleLabel.font = [UIFont fontWithName:#"Montserrat-Regular" size:16.5];
self.readingTimeView.backgroundColor = [UIColor colorWithRed:0.33 green:0.74 blue:0.15 alpha:1];
self.readingTimeView.layer.cornerRadius = 4;
self.readingTimeLabel.textColor = [UIColor whiteColor];
self.readingTimeLabel.font = [UIFont fontWithName:#"Montserrat-Bold" size:11.75];
self.commentsCountView.backgroundColor = [UIColor colorWithRed:0.74 green:0.19 blue:0.4 alpha:1];
self.commentsCountView.layer.cornerRadius = 4;
self.commentsCountLabel.textColor = [UIColor whiteColor];
self.commentsCountLabel.font = [UIFont fontWithName:#"Montserrat-Bold" size:11.75];
}
I tried to style the cell from the initWithStyle method of the UITableViewCell, but for some reason it never gets called, so i ended up doing this in awakeFromNib.
So, the problem is: I think i am doing something wrong, because as you can see in this GIF (https://dl.dropboxusercontent.com/s/6crcjbmitr5fmk7/Untitled%20%281%29.gif?m=), the heart button it get's automatically turned on/off as i scroll through the cells.
Can anyone of you guys help me fix this ? Thanks a lot!
Each time the cell is displayed it takes value from your PostsArray array. So when you click on the heart, you should update its corresponding object in the array.
That happens because you have the same CellIdentifier for all your cells, which is fine in this case if you are very careful on how you are handling the "heart" element.
You should have a way to determine which elements on the cell have been "liked". As I suppose you need to know on which elements your user pressed the heart button.
When initialising/reusing your cell you need to be sure that the heart button is set properly to "red/on" or "white/off".
For instance :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
PostTableViewCell *cell = (PostTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[PostTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[cell setHeartState:[[AnArray objectAtIndex:indexPath.row] hasBeenLikedByUser]]
[cell setCellContentWithPost:[PostsArray objectAtIndex:indexPath.row]];
return cell;
}
It is just a quick draft.
Hope that helps
I managed to solve this by checking if the hearth should be filled or not. I stored the post id using NSUserDefaults and in setCellContentWithPost i added an if statement that checks if the cell's post is favorited.
Thanks for interest guys!
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];
}