我现在遇到了个问题,如何使uitableview cell 的背景色填满整个cell ,以下是我的代码。但它不能正常工作、、、
Picture shows:
How can I code that set the color fill in cell view?
My code is below, but it does not work well.
cell.contentView.backgroundColor =[UIColor groupTableViewBackgroundColor];
cell.backgroundColor=[UIColor groupTableViewBackgroundColor];
cell.backgroundView.backgroundColor =[UIColor groupTableViewBackgroundColor];
cell.textLabel.backgroundColor = [UIColor groupTableViewBackgroundColor];
cell.detailTextLabel.backgroundColor = [UIColor groupTableViewBackgroundColor];
it works!
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
[tableView setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
return 1;
}
Related
In my cellForRowAtIndexPath method I am using following if statements:
if ([taskitem.isCritical isEqualToString:#"iscritical"]){
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor redColor];
}
else if ([taskitem.isUrgent isEqualToString:#"isurgent"]){
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor blueColor];
}
else if ([taskitem.isCompletedOK isEqualToString:#"iscompleted"]){
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor greenColor];
UIButton *doneButton4 = [[UIButton alloc]initWithFrame:CGRectMake(0, 10, 12, 12)];
[doneButton4 setImage:[UIImage imageNamed:#"done"]forState:UIControlStateNormal];
[cell addSubview:doneButton4];
}
else {
cell.textLabel.textColor = [UIColor blackColor];
}
cell.textLabel.text = taskitem.taskName;
The problem is that the if statements are changing their behaviour if the user taps on any of the sections headers.
Do you find any error on my code that could be the reason of this weird behaviour or should I search for another reason in another method?
It looks like you're probably having a cell reuse problem. You need to make sure that anything you do in one case, is done for each case (or undone).
For example, in first your three conditions, you set the text colour and the background colour. But in the fourth case (the else) you only set the text colour, leaving the background colour to be whatever it was on this cell the last time it was used. You need to set the background colour in the else case as well. (Optionally, you can set all items back to defaults before the if).
You have a second problem here, with the third case, that creates and adds a button to the cell. But in the other cases, you do not remove that button if it exists. So when you get a cell that was used for a completed item earlier, you'll end up with a button that doesn't belong. Even if the cell is used for another completed item, you'll get two buttons on the same cell. One on top of the other (so it likely won't be visible, but it's still a problem).
Try using this code:
UIButton* oldButton = [cell viewWithTag:253];
if (oldButton)
{
[oldButton removeFromSuperview];
}
if ([taskitem.isCritical isEqualToString:#"iscritical"]) {
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor redColor];
}
else if ([taskitem.isUrgent isEqualToString:#"isurgent"]) {
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor blueColor];
}
else if ([taskitem.isCompletedOK isEqualToString:#"iscompleted"]) {
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor greenColor];
UIButton *doneButton4 = [[UIButton alloc] initWithFrame:CGRectMake(0, 10, 12, 12)];
[doneButton4 setImage:[UIImage imageNamed:#"done"] forState:UIControlStateNormal];
doneButton4.tag = 253;
[cell addSubview:doneButton4];
}
else {
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.backgroundColor = [UIColor whiteColor];
}
cell.textLabel.text = taskitem.taskName;
I don't recommend using tags like this in production code, but it's a quick way to see if it solves your problems. In real code, use a UITableViewCell subclass with a property for the done button that you can just show and hide. Ideally you're not creating it each time as that's an expensive operation and could slow down your scrolling performance.
Create doneButton4 in (cell == nil) when reuse cells in table view
- (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];
UIButton *doneButton4 = [[UIButton alloc]initWithFrame:CGRectMake(0, 10, 12, 12)];
[doneButton4 setImage:[UIImage imageNamed:#"done"]forState:UIControlStateNormal];
[cell addSubview:doneButton4];
doneButton4.tag=100;
}
UIButton *doneButton4=(UIButton*)[cell viewWithTag:100];
doneButton4.hidden=YES;
if ([taskitem.isCritical isEqualToString:#"iscritical"]){
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor redColor];
}
else if ([taskitem.isCritical isEqualToString:#"isurgent"]){
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor blueColor];
}
else if ([taskitem.isCritical isEqualToString:#"iscompleted"]){
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor greenColor];
doneButton4.hidden=NO;
}
else {
cell.textLabel.textColor = [UIColor blackColor];
}
cell.textLabel.text = taskitem.taskName;
}
I have iphone app in which i am adding tableView and inside tableView cell i am adding the textfield but problem is that it shows line below the textfiled in tableView as shown in the image attached here is the my code.
I want to remove the line which is coming below the input filed Enter Tag Here
-(UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if(!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
// cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:#"tabelsales.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
//cell.backgroundColor = [UIColor clearColor];
}
for (UIView *subView in cell.subviews)
{
if (subView.tag == 2 || subView.tag == 22)
{
[subView removeFromSuperview];
}
}
tableView.backgroundView=nil;
if(indexPath.section==0){
tagInputField =[[UITextField alloc]initWithFrame:CGRectMake(0,1,249,28)];
tagInputField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
tagInputField.textAlignment=UITextAlignmentLeft;
tagInputField.backgroundColor=[UIColor whiteColor];
tagInputField.tag = 2;
tagInputField.delegate = self;
tagInputField.clearButtonMode = UITextFieldViewModeWhileEditing;
tagInputField.font=[UIFont fontWithName:#"Myriad-Pro" size:8];
[tagInputField setText:#"Enter tag here "];
tagInputField.textColor =[UIColor grayColor];
[cell addSubview:tagInputField];
if(tagArray.count >0)
{
[tagInputField setBorderStyle:UITextBorderStyleNone];
}
else {
[tagInputField setBorderStyle:UITextBorderStyleRoundedRect];
}
return cell;
}
if(indexPath.section==1) {
UIButton *crossButton =[[UIButton alloc]initWithFrame:CGRectMake(228, 8, 18, 18)];
crossButton.tag = 22; //use a tag value that is not used for any other subview
//crossButton.backgroundColor = [UIColor purpleColor];
crossButton.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Cross.png"]];
[cell addSubview:crossButton];
cell.textLabel.font =[UIFont fontWithName:#"Myriad-Pro" size:8];
cell.textLabel.textColor =[UIColor grayColor];
cell.backgroundColor=[UIColor whiteColor];
cell.textLabel.text =[tagArray objectAtIndex:indexPath.row];
[crossButton addTarget:self action:#selector(deleteCell:) forControlEvents:UIControlEventTouchUpInside];
[tagInputField setFrame:CGRectMake(8,1,240,30)];
tableView.backgroundColor=[UIColor whiteColor];
[publishButton setFrame:CGRectMake(40,560,250, 50)];
[descriptionTextImageView setFrame:CGRectMake(48,450,250,90)];
return cell;
}
Update:
1) set property of tableview separatorStyle as UITableViewCellSeparatorStyleNone.
2) set property of SeparatorColor like:
[self.tableView setSeparatorColor:[UIColor clearColor]];
3) other simple way to remove seprator is add custom seprator to simple UIView of 1px height:
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
separatorLineView.backgroundColor = [UIColor clearColor]; // as per your requirement set color.
[cell.contentView addSubview:separatorLineView];
4) write this delegate method of tableview:
- (float)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0.01f;// here remove your footer
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
// To "clear" the footer view
return [[UIView new] autorelease];
}
5) try this one:
self.tblView=[[UITableView alloc] initWithFrame:CGRectMake(0,0,320,460) style:UITableViewStylePlain];
self.tblView.delegate=self;
self.tblView.dataSource=self;
[self.view addSubview:self.tblView];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
v.backgroundColor = [UIColor clearColor];
[self.tblView setTableHeaderView:v];
[self.tblView setTableFooterView:v];
[v release];
It can be from xib. Set separator to None, as below screen shot show. Hope it will help. Thanks
You should set separatorStyle property of UITableView to UITableViewCellSeparatorStyleNone.
I am using a slide in menu style which loads a UITableView. - ECSlidingViewController
I have about 7 cells in a table view setup as follows:
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.contentView.backgroundColor = [UIColor colorWithRed:75.0/255.0 green:83.0/255.0 blue:102.0/255.0 alpha:1.0];
UIView *topSplitterBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.bounds.size.width, 1)];
topSplitterBar.backgroundColor = [UIColor colorWithRed:62.0/255.0 green:69.0/255.0 blue:85.0/255.0 alpha:1];
[cell.contentView addSubview:topSplitterBar];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor colorWithRed:196.0/255.0 green:204.0/255.0 blue:218.0/255.0 alpha:1];
cell.textLabel.font = [UIFont systemFontOfSize:18.0f];
cell.textLabel.shadowColor = [UIColor colorWithRed:27.0/255.0 green:31.0/255.0 blue:41.0/255.0 alpha:1];
cell.textLabel.shadowOffset = CGSizeMake(0, 1);
UIView *selectedBg = [[UIView alloc] initWithFrame:cell.frame];
selectedBg.backgroundColor = [UIColor colorWithRed:50.0/255.0 green:56.0/255.0 blue:73.0/255.0 alpha:1];
cell.selectedBackgroundView = selectedBg;
What would be the best way to show a cell as the selectedBg if that is the currently displayed controller?
I can access the following for example:
if ([self.slidingViewController.topViewController isKindOfClass:[MESHomeViewController class]]) {
However, I am not sure where would be best practice to set this up? I can do it in the switch case for the cell label setup... For example:
switch ( indexPath.row ) {
case 0: {
if ([self.slidingViewController.topViewController isKindOfClass:[MESHomeViewController class]]) {
cell.contentView.backgroundColor = [UIColor colorWithRed:50.0/255.0 green:56.0/255.0 blue:73.0/255.0 alpha:1];
}
cell.textLabel.text = NSLocalizedString(#"LMGames", #"Left Menu - Games");
break ;
However, when a new item is selected from the menu I would need to reload the table each time, is that good? Completing a self.tableView reloadData each time a cell is selected, or is there a better way to approach this?
Two ideas for you:
Set the selectedBg in the didSelectRowAtIndexPath method in order to set the selected cell.
Keep an integer reference to the currently selected row and the previously selected row and then refresh only those rows by using a method similar to: How to reload and animate just one UITableView cell/row?
I hope that helps!
I have tried everything to get this working, but when I scroll cellForRowAtIndexPath, it seems to be adding and removing checkmarks on my tableview, can anyone help?
// Configure the cell...
cell.textLabel.text = [currencyList objectAtIndex:indexPath.row];
// Check to see what currency is currently selected and checkstyle
if ([selectedCurrency isEqualToString:cell.textLabel.text]) {
// Add a tick to the selected row
cell.accessoryType = UITableViewCellAccessoryCheckmark;
// Change the text colour
cell.textLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0.5 alpha:1];
self.checkedIndexPath = indexPath;
} else {
UITableViewCell *uncheckCell = [self.tableView
cellForRowAtIndexPath:self.checkedIndexPath];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
// Change the font colour back to black
uncheckCell.textLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
}
You don't need to uncheck cell in cellForRowAtIndexPath. Better sollution is just reload checked cell.
- (void)setCheckedIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath *uncheckIndexPath = self.checkedIndexPath;
_checkedIndexPath = indexPath;
[self.tableView reloadRowsAtIndexPaths:#[uncheckIndexPath]
withRowAnimation:UITableViewRowAnimationLeft];
}
And change cellForRowAtIndexPath. Please note that we set UITableViewCellAccessoryNone to cell that we must return, not for cell that you want to uncheck.
// Configure the cell...
cell.textLabel.text = [currencyList objectAtIndex:indexPath.row];
// Check to see what currency is currently selected and checkstyle
if ([selectedCurrency isEqualToString:cell.textLabel.text]) {
// Add a tick to the selected row
cell.accessoryType = UITableViewCellAccessoryCheckmark;
// Change the text colour
cell.textLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0.5 alpha:1];
self.checkedIndexPath = indexPath;
} else {
//If cell unchecked
cell.accessoryType = UITableViewCellAccessoryNone;
// Change the font colour back to black
cell.textLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
}
I got an app with UITableView. Every cell has accessory. But i cant set backgroundColor ti this cells.
I tried to set backgrounColors in this ways:
cell.contentView.backgroundColor =[UIColor redColor];
cell.backgroundView.backgroundColor = [UIColor redColor];
cell.backgroundColor = [UIColor redColor];
But only contentView work. How can i do it? Thnx
the second way cell.backgroundView.backgroundColor = [UIColor redColor]; is not that wrong. Unfortunately there is no backgroundView until you create one. You have to create an UIView and set it as background of the cell.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:#"Cell"];
UIView *background = [[UIView alloc] initWithFrame:CGRectZero];
background.backgroundColor = [UIColor redColor];
cell.backgroundView = background;
}