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];
Related
I'm implementing a UITableView with text-views as the content view of my cells.
The data that the user enters is saved in a settings dictionary when the user hits the return key:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == _textFieldOne){
[settingsDictionary setObject: _textFieldOne.text forKey:#"EntryOneText"];
[settingsDictionary stringValueForKey:#"textFieldOneValue"];
[self postNotificationSettingsUpdate:settingsDictionary];
didTestPostNotificationSettings = YES;
}
These saved values should be displayed in the text-field when the user returns back to the screen, which I've done using the code below:
//Set the value in the text fields from the settings dictionary
NSString *textFieldOneText = [self.settingsDictionary stringValueForKey:#"RedZoneCarsPerManHrMin"];
_textFieldOne.text = textFieldOneValue;
So far, everything seems to work as expected: text input is saved, and shown in the text-field when the user returns to screen. However, if the row of the TableView that holds the text-field is select (not the text-field itself), the text-field displays the behavior shown below:
It appears as if the text-field is showing both the newly inputted entry, as well as the entry that was last saved.
EDIT I'ved added more of my cellForRowAtIndexPath method below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForSubscribedRedZoneAlertRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"RedZoneAlertCell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
cell.backgroundColor = [UIColor colorWithRed:0.922 green:0.937 blue:0.949 alpha:1];
UISwitch *cellSwitch = nil;
NSNumber *position = enabledRedZoneAlertRows[indexPath.row];
switch (position.integerValue) {
case CarPerManHourRow: {
cell.textLabel.text = NSLocalizedString(#"Label", #"Label Row");
cellSwitch = [[UISwitch alloc] init];
cellSwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey:SwitchKey];
cellSwitch.tag = SwitchTag;
[cellSwitch addTarget:self action:#selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView = cellSwitch;
if ([[self.settingsDictionary valueForKey:#"RedZoneCarsPerManHrOnOff"]isEqual: #"1"]){
[cellSwitch setOn:YES];
}
break;
}
case TextFieldRow: {
static NSString *CellIdentifier = #"ConfigCell";
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
[cell.contentView addSubview:_textFieldOne];
[cell.contentView addSubview:_textFieldTwo];
[cell.contentView addSubview:_spacingLabel];
}
cell.backgroundColor = [UIColor colorWithRed:0.922 green:0.937 blue:0.949 alpha:1];
_textFieldOne = [[UITextField alloc]initWithFrame:CGRectMake(25, 0, 50, 30)];
_textFieldTwo = [[UITextField alloc]initWithFrame:CGRectMake(100, 0, 50, 30)];
_textFieldOne.delegate = self;
_textFieldTwo.delegate = self;
_textFieldOne.placeholder = #"Auto";
_textFieldTwo.placeholder = #"Auto";
[_textFieldOne setTextAlignment:NSTextAlignmentCenter];
[_textFieldTwo setTextAlignment:NSTextAlignmentCenter];
//Set the value in the text fields from the settings dictionary
NSString *textFieldOneText = [self.settingsDictionary stringValueForKey:#"RedZoneCarsPerManHrMin"];
NSString *textFieldTwoText = [self.settingsDictionary stringValueForKey:#"RedZoneCarsPerManHrMax"];
NSString *carsPerManHrMax = [self.settingsDictionary stringValueForKey:#"RedZoneCarsPerManHrMax"];
NSString *carsPerManHrMax = [self.settingsDictionary stringValueForKey:#"RedZoneCarsPerManHrMax"];
_textFieldOne.text = textFieldOneValue;
_textFieldTwo.text = textFieldTwoValue
_textFieldOne.backgroundColor = [UIColor whiteColor];
_textFieldTwo.backgroundColor = [UIColor whiteColor];
_textFieldOne.tintColor = [UIColor blackColor];
_textFieldTwo.tintColor = [UIColor blackColor];
[_textFieldOne.layer setCornerRadius:7.0f];
[_textFieldTwo.layer setCornerRadius:7.0f];
[_textFieldOne setKeyboardType:UIKeyboardTypeNumbersAndPunctuation];
[_textFieldTwo setKeyboardType:UIKeyboardTypeNumbersAndPunctuation];
_textFieldOne.returnKeyType = UIReturnKeyNext;
_textFieldTwo.returnKeyType = UIReturnKeyDone;
_spacingLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 0, 35, 30)];
_spacingLabel.text = #"–";
_spacingLabel.textColor = [UIColor colorWithRed:0.658 green:0.658 blue:0.658 alpha:1];
[_spacingLabel setTextAlignment:NSTextAlignmentCenter];
_spacingLabel.backgroundColor = [UIColor clearColor];
//[cell.contentView addSubview:_textFieldOne];
//[cell.contentView addSubview:_textFieldTwo];
//[cell.contentView addSubview:_spacingLabel];
break;
}
return cell;
}
EDIT TWO
Below is my attempt at fixing my issue based on jherran's answer. However, I am still experiencing the same problem.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForSubscribedRedZoneAlertRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"RedZoneAlertCell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
cell.backgroundColor = [UIColor colorWithRed:0.922 green:0.937 blue:0.949 alpha:1];
UISwitch *cellSwitch = nil;
NSNumber *position = enabledRedZoneAlertRows[indexPath.row];
switch (position.integerValue) {
case CarPerManHourRow: {
cell.textLabel.text = NSLocalizedString(#"Label", #"Label Row");
cellSwitch = [[UISwitch alloc] init];
cellSwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey:SWSettingCarsPerManHour];
cellSwitch.tag = SaveCarsPerManHourTag;
[cellSwitch addTarget:self action:#selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView = cellSwitch;
if ([[self.settingsDictionary valueForKey:#"RedZoneCarsPerManHrOnOff"]isEqual: #"1"]){
[cellSwitch setOn:YES];
}
break;
}
case CarsPerManHourConfigRow: {
static NSString *CellIdentifier = #"ConfigCell";
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.backgroundColor = [UIColor colorWithRed:0.922 green:0.937 blue:0.949 alpha:1];
_carsPerManHourMinTextField = [[UITextField alloc]initWithFrame:CGRectMake(25, 0, 50, 30)];
_carsPerManHourMaxTextField = [[UITextField alloc]initWithFrame:CGRectMake(100, 0, 50, 30)];
_textFieldOne.delegate = self;
_textFieldTwo.delegate = self;
_textFieldOne.placeholder = #"Auto";
_textFieldTwo.placeholder = #"Auto";
[_textFieldOne setTextAlignment:NSTextAlignmentCenter];
[_textFieldTwo setTextAlignment:NSTextAlignmentCenter];
_textFieldOne.backgroundColor = [UIColor whiteColor];
_textFieldTwo.backgroundColor = [UIColor whiteColor];
_textFieldOne.tintColor = [UIColor blackColor];
_textFieldTwo.tintColor = [UIColor blackColor];
[_textFieldOne.layer setCornerRadius:7.0f];
[_textFieldTwo.layer setCornerRadius:7.0f];
[_textFieldOne setKeyboardType:UIKeyboardTypeNumbersAndPunctuation];
[_carsPerManHourMaxTextField setKeyboardType:UIKeyboardTypeNumbersAndPunctuation];
_textFieldOne.returnKeyType = UIReturnKeyNext;
_textFieldTwo.returnKeyType = UIReturnKeyDone;
_spacingLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 0, 35, 30)];
_spacingLabel.text = #"–";
_spacingLabel.textColor = [UIColor colorWithRed:0.658 green:0.658 blue:0.658 alpha:1];
[_spacingLabel setTextAlignment:NSTextAlignmentCenter];
_spacingLabel.backgroundColor = [UIColor clearColor];
NSLog(#"%#", _textFieldOne.text);
NSLog(#"%#",_carsPerManHourMaxTextField.text);
[cell.contentView addSubview: _textFieldOne];
[cell.contentView addSubview: _textFieldTwo];
[cell.contentView addSubview:_spacingLabel];
cell.tag = 1;
}
else {
_textFieldOne = (UITextField *)[cell.contentView viewWithTag:1];
_textFieldTwo = (UITextField *)[cell.contentView viewWithTag:1];
_spacingLabel = (UILabel *)[cell.contentView viewWithTag:1];
}
NSString * _textFieldOne = [self.settingsDictionary stringValueForKey:#"RedZoneCarsPerManHrMin"];
NSString *carsPerManHrMax = [self.settingsDictionary stringValueForKey:#"RedZoneCarsPerManHrMax"];
_textFieldOne.text = carsPerManHrMin;
_textFieldTwo.text = carsPerManHrMax;
How do I modify my code so that this behavior does not occur?
Check your cellForRowAtIndexPath, as cells are reused, you are probably not loading the cell property.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
/*
* This is an important bit, it asks the table view if it has any available cells
* already created which it is not using (if they are offscreen), so that it can
* reuse them (saving the time of alloc/init/load from xib a new cell ).
* The identifier is there to differentiate between different types of cells
* (you can display different types of cells in the same table view)
*/
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyIdentifier"];
/*
* If the cell is nil it means no cell was available for reuse and that we should
* create a new one.
*/
UILabel *label;
if (cell == nil) {
/*
* Actually create a new cell (with an identifier so that it can be dequeued).
*/
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"MyIdentifier"] autorelease];
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
cell.tag = 1;
[cell.contentView addSubview:label];
} else {
label = (UILabel *)[cell.contentView viewWithTag:1];
}
/*
* Now that we have a cell we can configure it to display the data corresponding to
* this row/section
*/
label.text = #"whatever";
}
EDIT: When your cell exits and it's reused, you are adding a view ([cell.contentView addSubview:_textFieldOne]) each time the cell is reused.
Ok, it is as I suspected. Every time the cell is taken out out the reuse queue, you create a new instance of textfield and add it to the content view. So basically the content view is piling up with new textfields.
What you should do is
1) to have a custom cell and make the cell the sole class responsible for its internals
2) expose only a configuration method to pass a raw text to cell
3) call that configuration method when thr cell is reused.
4) keep a reference to the textfield inside the custom cell to be able to update the text value any time incl. when it is recycled in cellforrowatindexpath
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];
I'm trying to use a check mark in the UITablewViewCell which is a custom cell that has 3 labels. The problem is that no matter what resolution or dimensions I use for the check mark image, it's looks pixelated as in the image below. I tried 40x40, 60x60 and 80x80 pixels and also 72 Dpi and 300 Dpi with no success. Here is the code for the custom cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyCell";
//UILabel *mainLabel, *secondLabel, *thirdLabel;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryNone;
//Add the 3 labels
mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(40.0, 5.0, 180.0, 21.0)];
mainLabel.tag = MAINLABEL_TAG;
//mainLabel.font = [UIFont systemFontOfSize:14.0];
//mainLabel.font = [UIFont fontWithName:#"Geeza Pro Bold" size:17.0];
mainLabel.font = [UIFont boldSystemFontOfSize:13];
mainLabel.textAlignment = UITextAlignmentLeft;
mainLabel.textColor = [UIColor blackColor];
mainLabel.highlightedTextColor = [UIColor whiteColor];
mainLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:mainLabel];
secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(40.0, 21.0, 190.0, 21.0)];
secondLabel.tag = SECONDLABEL_TAG;
//secondLabel.font = [UIFont systemFontOfSize:12.0];
//secondLabel.font = [UIFont fontWithName:#"Geeza Pro" size:15.0];
secondLabel.font = [UIFont systemFontOfSize:13];
secondLabel.textAlignment = UITextAlignmentLeft;
secondLabel.textColor = [UIColor darkGrayColor];
secondLabel.highlightedTextColor = [UIColor whiteColor];
secondLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:secondLabel];
thirdLabel = [[UILabel alloc] initWithFrame:CGRectMake(240.0, 11.0, 70.0, 21.0)];
thirdLabel.tag = THIRDLABEL_TAG;
//thirdLabel.font = [UIFont systemFontOfSize:12.0];
//thirdLabel.font = [UIFont fontWithName:#"Geeza Pro Bold" size:17.0];
thirdLabel.font = [UIFont boldSystemFontOfSize:13];
thirdLabel.textAlignment = UITextAlignmentRight;
thirdLabel.textColor = [UIColor blackColor];
thirdLabel.highlightedTextColor = [UIColor whiteColor];
thirdLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:thirdLabel];
}
else
{
mainLabel = (UILabel *)[cell.contentView viewWithTag:MAINLABEL_TAG];
secondLabel = (UILabel *)[cell.contentView viewWithTag:SECONDLABEL_TAG];
thirdLabel = (UILabel *)[cell.contentView viewWithTag:THIRDLABEL_TAG];
}
//If the requesting table view is the search display controller's table view, configure the cell using the filtered content, otherwise use the main list.
Car *car = nil;
if (tableView == self.searchDisplayController.searchResultsTableView)
{
car = [self.filteredListContent objectAtIndex:indexPath.row];
}
else
{
car = [self.listContent objectAtIndex:indexPath.row];
}
NSString *carName = [NSString stringWithFormat:#"%#",car.deviceName];
NSString *carDetails = [NSString stringWithFormat:#"%# at %#",car.date,car.location];
NSString *carSpeed = [NSString stringWithFormat:#"%# km/h",car.speed];
mainLabel.text = carName;
secondLabel.text = carDetails;
thirdLabel.text = carSpeed;
//Check for selection
if (car.isSelected == YES)
{
[cell.imageView setImage:[UIImage imageNamed:#"new_checkmark.png"]];
}
else
{
[cell.imageView setImage:nil];
}
return cell;
}
And here is the screen shot:
create two images named new_checkmark.png & new_checkmark#2x.png.
new_checkmark#2x.png must be doubled dimension of new_checkmark.png in proportion.
in coding dont use extension, onlu give name like this
[cell.imageView setImage:[UIImage imageNamed:#"new_checkmark"]];
I am creating grouped table in my application.
It works fine. But when I scroll the table upward and backward reprinted text on previous cell. It look like horrible.
This is my Code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cellID"];
if (!cell)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"cellID"] autorelease];
}
/*
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
*/
if(indexPath.section == 0)
{
cell.backgroundColor = [UIColor clearColor];
cell.accessoryType = UITableViewCellAccessoryNone;
float rowHeight = [DetailViewController calculateHeightOfTextFromWidth:[dict objectForKey:#"golf_name"] :[UIFont boldSystemFontOfSize:18] :290 :UILineBreakModeTailTruncation];
UILabel *categorName = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, 290, rowHeight)];
categorName.font = [UIFont boldSystemFontOfSize:20];
categorName.numberOfLines = 5;
categorName.backgroundColor = [UIColor clearColor];
categorName.textColor = [UIColor whiteColor];
categorName.text = [dict objectForKey:#"golf_name"];
[cell addSubview:categorName];
[categorName release];
}
if(indexPath.section == 1)
{
cell.backgroundColor = [UIColor whiteColor];
cell.accessoryType = UITableViewCellAccessoryNone;
UILabel *categorName = [[UILabel alloc] initWithFrame:CGRectMake(50, 10, 70, 20)];
categorName.font = [UIFont boldSystemFontOfSize:15];
categorName.numberOfLines = 5;
categorName.backgroundColor = [UIColor clearColor];
categorName.textColor = [UIColor colorWithRed:145.0f/255.0f green:162.0f/255.0f blue:184.0f/255.0f alpha:1];
categorName.text = #"Phone";
[cell addSubview:categorName];
[categorName release];
UILabel *phone = [[UILabel alloc] initWithFrame:CGRectMake(110, 10, 290, 20)];
phone.font = [UIFont boldSystemFontOfSize:15];
phone.numberOfLines = 5;
phone.backgroundColor = [UIColor clearColor];
//categorName.textColor = [UIColor colorWithRed:145.0f/255.0f green:162.0f/255.0f blue:184.0f/255.0f alpha:1];
phone.text = [dict objectForKey:#"golf_phone"];
[cell addSubview:phone];
[phone release];
}
if(indexPath.section == 2)
{
cell.backgroundColor = [UIColor whiteColor];
cell.accessoryType = UITableViewCellAccessoryNone;
UILabel *categorName = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 90, 20)];
categorName.font = [UIFont boldSystemFontOfSize:15];
categorName.numberOfLines = 5;
categorName.backgroundColor = [UIColor clearColor];
categorName.textColor = [UIColor colorWithRed:145.0f/255.0f green:162.0f/255.0f blue:184.0f/255.0f alpha:1];
categorName.text = #"Home page";
[cell addSubview:categorName];
[categorName release];
UILabel *phone = [[UILabel alloc] initWithFrame:CGRectMake(110, 10, 230, 20)];
phone.font = [UIFont boldSystemFontOfSize:15];
phone.numberOfLines = 5;
phone.backgroundColor = [UIColor clearColor];
//categorName.textColor = [UIColor colorWithRed:145.0f/255.0f green:162.0f/255.0f blue:184.0f/255.0f alpha:1];
phone.text = [dict objectForKey:#"golf_url"];
[cell addSubview:phone];
[phone release];
}
.......
.....
return cell;
}
Update
My original answer was not the correct way to reuse UITableViewCell's. You should never use "CellID%d%d"(indexPath.row & indexPath.column) as cell identifier, that defeats the very purpose of cell reuse. It will create separate UITableViewCell for every row in the table, and every one of those cells stays in memory. Imagine what will happen if you have a tableView with around 1000 rows.
The real answer to OP's question is - make sure to reset a UITableViewCell returned by dequeueReusableCellWithIdentifier because it might return a cell that is already used and its UI populated. For example, if you didn't reset the texts in UILabel, it might contain strings that should have been shown in a different row of your tableView.
My original Answer (Don't use this)
Hiren, just try,
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:#"CellID%d%d",indexPath.section,indexPath.row]];
if (!cell){
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:[NSString stringWithFormat:#"CellID%d%d",indexPath.section,indexPath.row]] autorelease];
}
instead of
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cellID"];
if (!cell)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"cellID"] autorelease];
}
Please read this..you will understand how to give cell identifiers..
When you create the UITableView object, Set the rowHeight for each cell.
myTableView.rowHeight = 50; //Depend on your requirement,
Let me know if you still struggle to get the desire result.
If your cell's height is not constant then implement heightForRowAtIndexPath method,
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
And return different-2 height for each cell.
try adding any color because clearcolor is making label transparent.
I have a UITableView and I've subclassed UITableViewCell (called it CustomCell) so it has several labels and a UIImageView.
Only certain cells will actually display an image. Here's my code for tableView:cellForRowAtIndexPath::
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Match *aMatch = [[appDelegate.matchByDate objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
cell.homeLabel.text = aMatch.homeTeam;
cell.awayLabel.text = aMatch.awayTeam;
cell.timeLabel.text = aMatch.koTime;
cell.tournamentLabel.text = aMatch.tournament;
NSString *tempString = [appDelegate.teamLogos objectForKey:[aMatch homeTeam]];
if (tempString!=nil) {
cell.homeImageView.image = [UIImage imageNamed:tempString];
}
return cell;
}
So it only sets the homeImageView when it finds a corresponding image in a dictionary I have set up. This seems to work for the first few cells, but if I scroll through the list I find cells have an image when they shouldn't have one.
I understand this is probably because of the cell being re-used, but I'm setting the homeImageView after the cell has been created/reused?!
Here's the init method from my CustomCell Class
- (id)initWithStyle:(UITableViewCellStyle)style
reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
// Initialization code
tournamentLabel = [[UILabel alloc] init];
tournamentLabel.textAlignment = UITextAlignmentCenter;
tournamentLabel.font = [UIFont systemFontOfSize:12];
tournamentLabel.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
tournamentLabel.textColor = [UIColor darkGrayColor];
homeLabel = [[UILabel alloc]init];
homeLabel.textAlignment = UITextAlignmentLeft;
homeLabel.font = [UIFont systemFontOfSize:16];
homeLabel.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
awayLabel = [[UILabel alloc]init];
awayLabel.textAlignment = UITextAlignmentRight;
awayLabel.font = [UIFont systemFontOfSize:16];
awayLabel.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
timeLabel = [[UILabel alloc]init];
timeLabel.textAlignment = UITextAlignmentCenter;
timeLabel.font = [UIFont systemFontOfSize:30];
timeLabel.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
timeLabel.textColor = [UIColor darkGrayColor];
homeImageView = [[UIImageView alloc]init];
awayImageView = [[UIImageView alloc]init];
[self.contentView addSubview:homeLabel];
[self.contentView addSubview:awayLabel];
[self.contentView addSubview:timeLabel];
[self.contentView addSubview:tournamentLabel];
[self.contentView addSubview:homeImageView];
[self.contentView addSubview:awayImageView];
}
return self;
}
You have to clear the image view if you have no image to display:
...
if (tempString!=nil) {
cell.homeImageView.image = [UIImage imageNamed:tempString];
} else {
cell.homeImageView.image = nil;
}
...
Optionally, you can implement prepareForReuse:
In Swift:
override func prepareForReuse() {
cell.homeImageView.image = nil;
}