Second TableView is not showing on the screen - ios

I have two tableviews in a menu controller. first tableview populates a dynamic menu list from db and second tableview should only display the strings I tell it. So right now I only need 2 cells, Settings and Login. The first table view works fine. But, the second is not displaying the items. code bellow represent the second tableview
ViewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
[self.slidingViewController setAnchorRightRevealAmount:280.0f];
self.slidingViewController.underLeftWidthLayout = ECFullWidth;
self.view.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
self.tableView.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
self.tableView.separatorColor = [UIColor colorWithWhite:0.15f alpha:0.2f];
self.extraTableView.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
self.extraTableView.separatorColor = [UIColor colorWithWhite:0.15f alpha:0.2f];
}
Main table
-(void)setMenuItems:(NSArray *)menuItems
{
if(_menuItems != menuItems)
{
_menuItems = menuItems;
}
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
return self.menuItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"MenuItemCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
Department *dept = [self.menuItems objectAtIndex:indexPath.row];
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = dept.name;
cell.textLabel.textColor = [UIColor whiteColor];
cell.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = myBackView;
return cell;
}
Second table
-(void)setExtraMenuItems:(NSArray *)extraMenuItems
{
if(_extraMenuItems != extraMenuItems)
{
_extraMenuItems = extraMenuItems;
}
[self.extraTableView reloadData];
}
- (NSInteger)extraTableView:(UITableView *)extraTableView numberOfRowsInSection:(NSInteger)sectionIndex
{
return self.extraMenuItems.count;
}
- (UITableViewCell *)extraTableView:(UITableView *)extraTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Formal";
UITableViewCell *cell = [extraTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
[_extraMenuItemFiller addObject:#"Settings"];
[_extraMenuItemFiller addObject:#"Logout"];
NSString *cellValue = [_extraMenuItemFiller objectAtIndex:indexPath.row];
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = cellValue;
cell.textLabel.textColor = [UIColor blackColor];
cell.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = myBackView;
return cell;
}
What is wrong with it?

You shouldn't rename the tableView delegate and datasource methods: just test the tableView parameter that is passed to them, to determine which tableView they relate to. For example:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
if (tableView == self.extraTableView) {
return self.extraMenuItems.count;
} else {
return self.menuItems.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.extraTableView) {
NSString *CellIdentifier = #"Formal";
UITableViewCell *cell = [extraTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
[_extraMenuItemFiller addObject:#"Settings"];
[_extraMenuItemFiller addObject:#"Logout"];
NSString *cellValue = [_extraMenuItemFiller objectAtIndex:indexPath.row];
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = cellValue;
cell.textLabel.textColor = [UIColor blackColor];
cell.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = myBackView;
return cell;
} else {
NSString *cellIdentifier = #"MenuItemCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
Department *dept = [self.menuItems objectAtIndex:indexPath.row];
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = dept.name;
cell.textLabel.textColor = [UIColor whiteColor];
cell.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = myBackView;
return cell;
}
}
And likewise for all the other tableView delegate and datasource methods. You also need to make sure that the delegate and datasource are set for both table views. You can do either do this in your storyboard, or in code eg. in viewDidLoad:
self.extraTableView.delegate = self;
self.extraTableView.datasource = self;
EDIT
You don't need both extraMenuItems and extraMenuItemFiller. I would use just extraMenuItems. Load it with the two values in viewDidLoad as follows:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.slidingViewController setAnchorRightRevealAmount:280.0f];
self.slidingViewController.underLeftWidthLayout = ECFullWidth;
self.view.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
self.tableView.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
self.tableView.separatorColor = [UIColor colorWithWhite:0.15f alpha:0.2f];
self.extraTableView.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
self.extraTableView.separatorColor = [UIColor colorWithWhite:0.15f alpha:0.2f];
self.extraMenuItems = #[#"Login",#"Settings"];
self.extraTableView.delegate = self;
self.extraTableView.datasource = self;
}
and amend the cellForRowAtIndexPath to use extraMenuItems rather than extraMenuItemFiller:
NSString *cellValue = [self.extraMenuItems objectAtIndex:indexPath.row];

Related

UITableView change cells

I have a UITableView with this code below:
- (UITableViewCell *)tableView:(UITableView *)tableViews cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyCell";
TableViewCell *cell = [tableViews dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
if (indexPath.row == 0){
cell.image.image = [UIImage imageNamed:#"male80.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"Phone" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if(indexPath.row == 1) {
cell.image.image = [UIImage imageNamed:#"male80.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"Mobile Phone" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 2) {
cell.image.image = [UIImage imageNamed:#"gift41.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"E-mail" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 3) {
cell.image.image = [UIImage imageNamed:#"gender.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"address" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 4) {
cell.image.image = [UIImage imageNamed:#"gender.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"country" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 5) {
cell.image.image = [UIImage imageNamed:#"gender.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"city" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 6) {
cell.image.hidden = YES;
cell.text.hidden = YES;
UITextView *view = [[UITextView alloc] initWithFrame:CGRectMake(15, 10, cell.frame.size.width - 18, cell.frame.size.height - 15)];
view.text = #"Text Example";
view.textColor = [UIColor whiteColor];
view.backgroundColor = [UIColor clearColor];
view.tag = 13;
view.layer.borderWidth = 0.5f;
view.layer.cornerRadius = 4;
view.layer.borderColor = [[UIColor grayColor] CGColor];
[cell addSubview:view];
}
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row != 6) {
return 65;
}else{
return 204;
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 7;
}
This code is very simple, the problem with him is:
When I run my app first time, I can see my fields organized in the manner prescribed (Like I put in cellforRowAtIndexPath)
When I scroll down (put my fields out of window) and scroll to up again I see a problem!
The problem is that my table, create a UITextview in rows like 0, 1 and 2. But why this is happening? in my code I made it clear! That only the row 6 will be created a UITextView!
How can I solve this problem?
Two issues:
1) your cell is recycled, this means when the cell gets to row 6 your UITextView is added to the cell and then when scrolling back the UITextView is still there
2) and you should add the subviews of cell in its contentView and not the cell itself.
Solution :
Use tow kind of UITableViewCell, one specific for row 6 and another one for the other rows. Register the cells and then dequeue them for the appropriate indexPath.
I think your problem is loading your NIB file into the table view.
Look into Assertion failure in dequeueReusableCellWithIdentifier:forIndexPath:
I tried to recreate your problem by creating a UITableViewCell using Interface Builder called ItemCell and it works. Make sure you have this in your viewDidLoad
[super viewDidLoad];
//Load the NIB file
UINib *nib = [UINib nibWithNibName:#"ItemCell"
bundle:nil];
//Register this NIB, which contains the cell
[self.tableView registerNib:nib
forCellReuseIdentifier:#"ItemCell"];
Then replace
static NSString *CellIdentifier = #"MyCell";
TableViewCell *cell = [tableViews dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
with
ItemCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ItemCell"
forIndexPath:indexPath];
if (cell == nil) {
cell = [[ItemCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"ItemCell"];
}
You might want to also look into putting your images in a data structure to go with a MVC design pattern.
Give table Row 6 a different identifier than the others
static NSString *CellIdentifier = #"MyCell";
if (indexPath.row == 6){
CellIdentifier = #"MyCell6";
}
Even then you shouldn't be re allocing the textField everytime cellForRowAtIndexPath is called. That's the point of reusing the cell! But that's another conversation so look up how to properly recycle cells
Put this code in viewDidLoad and make you view a global variable.
UITextView *view = [[UITextView alloc] initWithFrame:CGRectMake(15, 10, cell.frame.size.width - 18, cell.frame.size.height - 15)];
view.text = #"Text Example";
view.textColor = [UIColor whiteColor];
view.backgroundColor = [UIColor clearColor];
view.tag = 13;
view.layer.borderWidth = 0.5f;
view.layer.cornerRadius = 4;
view.layer.borderColor = [[UIColor grayColor] CGColor];
And now create the tableView like this:
- (UITableViewCell *)tableView:(UITableView *)tableViews cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyCell";
TableViewCell *cell = [tableViews dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
view.hidden = YES;
if (indexPath.row == 0){
cell.image.image = [UIImage imageNamed:#"male80.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"Phone" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if(indexPath.row == 1) {
cell.image.image = [UIImage imageNamed:#"male80.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"Mobile Phone" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 2) {
cell.image.image = [UIImage imageNamed:#"gift41.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"E-mail" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 3) {
cell.image.image = [UIImage imageNamed:#"gender.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"address" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 4) {
cell.image.image = [UIImage imageNamed:#"gender.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"country" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 5) {
cell.image.image = [UIImage imageNamed:#"gender.png"];
cell.text.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"city" attributes:#{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
if (indexPath.row == 6) {
cell.image.hidden = YES;
cell.text.hidden = YES;
view.hidden = NO;
[cell addSubview:view];
}
return cell;
}

Programiticaly Multiple Columns in Tableview ios

I am trying to create a table with multiple columns, I am using array of cells.
Following is my code, I get single columns every time.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"Cell";
TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] ;
CGFloat x = 0.0f;
UIView *lastCol = [[cell columnCells] lastObject];
if (lastCol) x = lastCol.frame.origin.x + lastCol.frame.size.width;
for (int i = 0; i < 2; i++) {
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, i, 40.0f)] ;
UIView *gridCell = l;
CGRect f = gridCell.frame;
f.origin.x += x;
gridCell.frame = f;
[cell.contentView addSubview:gridCell];
CGFloat colWidth = [self widthForColumn:i];
x += colWidth + 1.0f;
[[cell columnCells] addObject:gridCell];
}
for (int i = 0; i < 2; i++) {
UILabel *l = (UILabel*)[cell columnCells][i];
l.text =self.department[indexPath.row];
}
return cell;
}
How about this?
// table view delegates
- (int)numberOfSectionsInTableView:(UITableView *) tableView {
return 1;
}
- (int) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section {
return 100;
}
-(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MainCell"];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"MainCell"];
double boxWidth = self.view.frame.size.width/3;
for (int i=0;i<=2;i++) {
UIView *mView = [[UIView alloc] initWithFrame:CGRectMake(boxWidth*i, 0, boxWidth, 100)];
if (i==0) {
mView.backgroundColor = [UIColor redColor];
} else if (i==1) {
mView.backgroundColor = [UIColor greenColor];
} else if (i==2) {
mView.backgroundColor = [UIColor blueColor];
}
[cell addSubview:mView];
}
cell.backgroundColor = [UIColor clearColor];
cell.contentView.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Thanks to Fahim, I just modified his code and got what I wanted.
Here's the exact requirement which I was looking for.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MainCell"];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"MainCell"];
double boxWidth = self.view.frame.size.width/3;
for (int i=0;i<=2;i++) {
UIView *mView = [[UIView alloc] initWithFrame:CGRectMake(boxWidth*i, 0, boxWidth, 100)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 250, 15)];
[label setText:self.department[indexPath.row]];
[cell addSubview:mView];
[mView addSubview:label];
}
cell.backgroundColor = [UIColor clearColor];
cell.contentView.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}

How to cache cell images in app to don't receive memory warning

I would like to know how to cache images from background cell to don't receive images. these images are used in array in the table view. in viewdidload is array which looks like this :
Cars *car1 = [Cars new];
car1.name = #"BMW 114i";
car1.price = #"28,000 $";
car1.horsepower = #"102hp (75kW)";
etc...
at the bottom of this viewdidload is array with objects and it is there about 400+ items so device has little bit problem with it so I got lots of memory warnings
Here is the code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CustomTableCell";
CarsCell *cell = (CarsCell *) [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell ==nil){
cell = [[CarsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Cars *cars = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
cars = [searchResults objectAtIndex:indexPath.row];
}else{
cars = [carss objectAtIndex:indexPath.row];
}
UIImage *backgroundImage = [UIImage imageNamed:cars.image];
UIImageView *imageView = [[UIImageView alloc] initWithImage:backgroundImage];
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;
imageView.alpha = 0.8f;
cell.backgroundView = imageView;
cell.nameLabel.text = cars.name;
cell.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.2f];
cell.contentView.backgroundColor = [UIColor clearColor];
cell.nameLabel.backgroundColor = [UIColor clearColor];
cell.nameLabel.textColor = [UIColor colorWithWhite:1.0f alpha:0.8f];
return cell;
}

How to get the UITAbleViewCell highlighted state to change the cell background?

I'm trying to change the cell background image when user tap on a cell (highlighted state), I've been trying this way but it's not working:
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.textLabel.textAlignment = NSTextAlignmentRight;
cell.detailTextLabel.textAlignment = NSTextAlignmentRight;
cell.textLabel.font = [UIFont fontWithName:kFONT_NAME size:kFONT_SIZE];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"row320x63.png"] highlightedImage:[UIImage imageNamed:#"row320x63_pressed.png"]];
}
- (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];
}
// Configure the cell...
tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"row320x63.png"]];
cell.textLabel.text = [self.listOfMenuSettings objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:#"settings_icon_%d", indexPath.row]];
UIImageView *pressed = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"row320x63_pressed.png"]];
[cell setSelectedBackgroundView:pressed];
return cell;
}
What I'm missing?
I don't find setSelectedBackgroundView: instance method in apple documentation. But there is a property selectedBackgroundView.So try:
cell.selectedBackgroundView = pressed;

uitableviewcell background color becomes clearcolor

I have problem with the cell background color becoming clearcolor always. I set the uiview background color to gray color, tableview background color to clear color and I did not set tableviewcell background color to clear color. But the cell background always appears grey. Can any one have any idea about this.
Thanks
-(void)viewDidLoad
{
self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"TableBackGround.png"]];
Acc_Details_TView.backgroundColor = [UIColor clearColor];
Acc_Details_TView.rowHeight = 40;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TransCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
switch ([Acc_Details_SegCtrl selectedSegmentIndex]) {
case 0:{
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"TableCell"] autorelease];
cell.backgroundColor =[UIColor clearColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
cell.detailTextLabel.text = [[transcationsList objectAtIndex:([indexPath row])] valueForKey:#"Date"];
cell.detailTextLabel.font = [UIFont systemFontOfSize:12];
}
NSString *titleName =[[transcationsList objectAtIndex:([indexPath row])] valueForKey:#"Title"] ;
if ([titleName length] > 19) {
cell.textLabel.text = [titleName substringWithRange:NSMakeRange(0, 20)];
}
else{
cell.textLabel.text = titleName;
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
UILabel * acc_Amount = [[UILabel alloc] initWithFrame:CGRectMake(220, 5, 60,10)];
acc_Amount.textAlignment = UITextAlignmentRight;
acc_Amount.backgroundColor = [UIColor clearColor];
acc_Amount.text = [[transcationsList objectAtIndex:([indexPath row])] valueForKey:#"Amount"];
acc_Amount.font = [UIFont boldSystemFontOfSize:14];
[cell.contentView addSubview:acc_Amount];
UILabel * balance_Amount = [[UILabel alloc] initWithFrame:CGRectMake(220, 23, 60,10)];
balance_Amount.textAlignment = UITextAlignmentRight;
balance_Amount.text = #"$1234.50";
balance_Amount.backgroundColor = [UIColor clearColor];
balance_Amount.textColor = [UIColor grayColor];
balance_Amount.font = [UIFont systemFontOfSize:12];
[cell.contentView addSubview:balance_Amount];
return cell;
}
}
}
Try setting your cell's background colour in the method
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
rather than in your cellForRowAtIndexPath: method.
Your cells are not exactly transparent. Setting UITableView's backgroundColor does some crazy undocumented stuff. Best way to see this is to set it to some semi-transparent color like [UIColor colorWithWhite:0.5 alpha:0.5] by which you get something like this:
To fix your problem, you will have to set cells' contentView.backgroundColor and backgroundColors of all the subviews after setting tableView's. Here is your cellForRowAtIndexPath: updated with this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TransCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIColor *cellBackgroundColor = [UIColor whiteColor];
switch ([Acc_Details_SegCtrl selectedSegmentIndex]) {
case 0:{
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"TableCell"] autorelease];
cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
cell.detailTextLabel.text = [[transcationsList objectAtIndex:([indexPath row])] valueForKey:#"Date"];
cell.detailTextLabel.font = [UIFont systemFontOfSize:12];
cell.accessoryView.backgroundColor = cellBackgroundColor;
cell.backgroundView.backgroundColor = cellBackgroundColor;
cell.contentView.backgroundColor = cellBackgroundColor;
cell.textLabel.backgroundColor = cellBackgroundColor;
cell.detailTextLabel.backgroundColor = cellBackgroundColor;
UIView *backView = [[UIView alloc] initWithFrame:cell.frame];
backView.backgroundColor = cellBackgroundColor;
cell.backgroundView = backView;
[backView release];
}
NSString *titleName =[[transcationsList objectAtIndex:([indexPath row])] valueForKey:#"Title"] ;
if ([titleName length] > 19) {
cell.textLabel.text = [titleName substringWithRange:NSMakeRange(0, 20)];
}
else{
cell.textLabel.text = titleName;
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
UILabel * acc_Amount = [[UILabel alloc] initWithFrame:CGRectMake(220, 5, 60,10)];
acc_Amount.textAlignment = UITextAlignmentRight;
acc_Amount.backgroundColor = cellBackgroundColor;
acc_Amount.text = [[transcationsList objectAtIndex:([indexPath row])] valueForKey:#"Amount"];
acc_Amount.font = [UIFont boldSystemFontOfSize:14];
[cell.contentView addSubview:acc_Amount];
UILabel * balance_Amount = [[UILabel alloc] initWithFrame:CGRectMake(220, 23, 60,10)];
balance_Amount.textAlignment = UITextAlignmentRight;
balance_Amount.text = #"$1234.50";
balance_Amount.backgroundColor = cellBackgroundColor];
balance_Amount.textColor = [UIColor grayColor];
balance_Amount.font = [UIFont systemFontOfSize:12];
[cell.contentView addSubview:balance_Amount];
}
}
return cell;
}
I didn't understand your question. You set the table's background to grey, then clear, but you didn't set it as clear, and then it appears grey, which you didn't want even though you set it as grey?
table.backgroundColor = [UIColor clearColor];

Resources