This question already has an answer here:
When I select a UITableViewCell, my view controller label is an action behind
(1 answer)
Closed 8 years ago.
I am having a strange issue in my UITableView. Whenever I press the first row of my UITableView, the didDeselectRowAtIndexPath method doesn't get called. My current code for it is as follows.
# pragma mark - UITableViewDelegate & UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.ary_tblDisplay count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 80;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"TableCell";
UIView *colorView,*backgroundView;
UILabel *lblCode,*lblStatus,*lblDate;
UITableViewCell *cell = (UITableViewCell *) [self.tbl_claims dequeueReusableCellWithIdentifier:CellIdentifier];
cell = nil;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
colorView = [[UIView alloc] initWithFrame:CGRectMake(7, 5, 5, 70)];
[cell addSubview:colorView];
backgroundView = [[UIView alloc] initWithFrame:CGRectMake(12, 5, 306, 70)];
backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"LightGreen"]];
[cell addSubview:backgroundView];
lblCode = [[UILabel alloc] initWithFrame:CGRectMake(20, 7, 140, 40)];
lblCode.text = [[self.ary_tblDisplay objectAtIndex:indexPath.row] claim_code];
lblCode.backgroundColor = [UIColor clearColor];
lblCode.textColor = [UIColor blueColor];
lblCode.font = [UIFont boldSystemFontOfSize:18];
[cell addSubview:lblCode];
lblStatus = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, 140, 35)];
lblStatus.backgroundColor = [UIColor clearColor];
lblStatus.text = [[self.ary_tblDisplay objectAtIndex:indexPath.row] claim_status];
[cell addSubview:lblStatus];
lblDate = [[UILabel alloc] initWithFrame:CGRectMake(150, 20, 120, 40)];
lblDate.backgroundColor = [UIColor clearColor];
lblDate.text = [[self.ary_tblDisplay objectAtIndex:indexPath.row] accepted_date];
[cell addSubview:lblDate];
if ([lblStatus.text isEqualToString:#"Rejected"])
colorView.backgroundColor = [UIColor redColor];
else if ([lblStatus.text isEqualToString:#"Accepted"])
colorView.backgroundColor = [UIColor greenColor];
else if ([lblStatus.text isEqualToString:#"In-Progress"])
colorView.backgroundColor = [UIColor blueColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundColor = [UIColor redColor];
return cell;
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
StatusViewController *obj_StatusViewController = [[StatusViewController alloc] initWithNibName:IS_IPHONE_5?#"StatusViewController":#"StatusViewController_3.5" bundle:nil];
obj_StatusViewController.details = [self.ary_tblDisplay objectAtIndex:indexPath.row];
[self.navigationController pushViewController:obj_StatusViewController animated:YES];
}
Do you really mean didDeselectRowAtIndexPath?
No wonder, this is expected only didDeselectRowAtIndexPath will call only when you deselect a row that means when you make a change of selection. I suspect you mistakenly chosen didDeselectRowAtIndexPath. You may want to use didSelectRowAtIndexPath method
Related
On the iPhone, the tableview looks fine, but in the iPad (simulator ios10), this is how it's shown:
Check image here: http://welove.pt/img/ipadtrouble.png
Any idea why its displaying differently on iPhone/iPad?
Also, what's with that white corner by the search icon? why isn't it black?
ViewDidLoad:
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.backgroundColor = [UIColor clearColor];
_tableView.separatorColor = [UIColor colorWithRed:58/255.0 green:58/255.0 blue:58/255.0 alpha:1.0];
_tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);
_tableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
[self.view addSubview:_tableView];
cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
UIView *selectionColor = [[UIView alloc] init];
selectionColor.backgroundColor = [UIColor colorWithRed:(54/255.0) green:(54/255.0) blue:(54/255.0) alpha:1];
cell.selectedBackgroundView = selectionColor;
cell.backgroundColor = [UIColor colorWithRed:(28/255.0) green:(28/255.0) blue:(28/255.0) alpha:1];
cell.textLabel.textColor = [UIColor whiteColor];
}
if (indexPath.section == 0) {
cell.imageView.image = [[UIImage imageNamed:#"defineLocation.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UILabel *ttitle = [[UILabel alloc] initWithFrame:CGRectMake(46, 12, 320, 20)];
ttitle.font = [UIFont systemFontOfSize:17];
ttitle.textColor = [UIColor colorWithRed:(115/255.0) green:(229/255.0) blue:(69/255.0) alpha:1.0];
[ttitle setText:NSLocalizedString(#"current_location", nil)];
[cell.contentView addSubview:ttitle];
} else {
cell.textLabel.text = [[_recentSearchData objectAtIndex:indexPath.row] objectForKey:#"recentTitle"];
}
return cell;
}
Thank you for your help.
Subclassing the UITableViewCell solved the alignment issues.
Regarding that white arrow when the view is popping over on the iPad, i managed to change it's color with:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationController.popoverPresentationController.backgroundColor = [UIColor colorWithRed:(23/255.0) green:(23/255.0) blue:(23/255.0) alpha:1.0];
}
this viewWillAppear belongs to the UITableViewController that is shown in the popover.
Thanks for your suggestions
I implemented TableView with ADLively TableView.
But if I scroll tableView, the cell's texts become to be piled up again and again....
Using "cell.textLabel" is good, adding UILabel is not good than that but if I use "cell.textLabel", I can't resize width of textLabel.
(I want to add UIImageView on the left and right side of text)
So now, I use the way to add UILabel.
What should I do?
Here is the code.
- (void)viewDidLoad {
CGRect rect = CGRectMake(0, 50, 320, self.view.frame.size.height-150);
self.tableView = [[ADLivelyTableView alloc]initWithFrame:rect style:UITableViewStylePlain];
self.tableView = (ADLivelyTableView *)self.tableView;
self.tableView.initialCellTransformBlock = ADLivelyTransformFan;
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview: self.tableView];
//self.tableView.backgroundColor = [UIColor blueColor];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"Cell"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.section count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
[self updateCell:cell atIndexPath:indexPath];
if (cell == nil){
myCellView = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"Cell"];
NSMutableArray *set = [self.section objectAtIndex:indexPath.row];
tableTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 7, 220, 30)];
tableTitleLabel.text = [set objectAtIndex:0];
[tableTitleLabel setNumberOfLines:0];
tableTitleLabel.backgroundColor = [UIColor clearColor];
tableTitleLabel.textColor = [UIColor blackColor];
tableTitleLabel.font = [UIFont fontWithName:#"HiraKakuProN-W3" size:15];
tableTitleLabel.adjustsFontSizeToFitWidth = YES;
tableTitleLabel.minimumScaleFactor = 10.0f;
[myCellView.contentView addSubview:tableTitleLabel];
}
NSMutableArray *set = [self.section objectAtIndex:indexPath.row];
[(UILabel *)[cell.contentView viewWithTag:1] setText:[set objectAtIndex:0]];
return cell;
}
- (void)updateCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
NSMutableArray *set = [self.section objectAtIndex:indexPath.row];
tableTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 7, 220, 30)];
tableTitleLabel.text = [set objectAtIndex:0];
[tableTitleLabel setNumberOfLines:0];
tableTitleLabel.backgroundColor = [UIColor clearColor];
tableTitleLabel.textColor = [UIColor blackColor];
tableTitleLabel.font = [UIFont fontWithName:#"HiraKakuProN-W3" size:15];
tableTitleLabel.adjustsFontSizeToFitWidth = YES;
tableTitleLabel.minimumScaleFactor = 10.0f;
[cell.contentView addSubview:tableTitleLabel];
}
It is not a good idea to keep adding subviews in cell on scroll but if you do not want to change the code that you have already written, use the following to remove all subviews of that cell before you call you updateCell method.
for (UIView *view in [cell subviews])
{
[view removeFromSuperview];
}
Hi i am beginner in Ios and in my project i have added some labels and image on table-list cell ok they have added so for everything is ok
Here my main requirement is when we tapped on table-list cell labels colors must be change and image need to be change as like below image
for this i have written some code but that's not working pleas help me
my code:-
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cells=#"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cells];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cells];
}
strip = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 7, cell.frame.size.height)];
strip.backgroundColor = [UIColor orangeColor];
[cell.contentView addSubview:strip];
TitleLbl = [[UILabel alloc] initWithFrame:CGRectMake(65, 7, 130, 35)];
TitleLbl.text =[Right_Menu_array objectAtIndex:indexPath.row];
TitleLbl.textColor=[UIColor darkGrayColor];
TitleLbl.font = [UIFont systemFontOfSize:15];
[cell.contentView addSubview:TitleLbl];
img = [[UIImageView alloc] initWithFrame:CGRectMake(10, 7, 35, 35)];
img.image = [UIImage imageNamed:[imageArr1 objectAtIndex:indexPath.row]];
[cell.contentView addSubview:img];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
TitleLbl.textColor = [UIColor redColor];
strip.backgroundColor = [UIColor redColr];
img.image = [UIImage imageNamed:#"rahul.png"];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
TitleLbl.textColor = [UIColor darkGrayColor];
strip.backgroundColor = [UIColor whiteColor];
img.image = [UIImage imageNamed:#"rahul.png"];
}
The easy way would be to create a custom UITableViewCell and then in the delegate functions of UITableView get the instance of CustomTableViewCell and set the Labels textColor.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath{
CustomTableViewCell *cell =(CustomTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.TitleLbl.textColor = [UIColor redColor];
cell.strip.backgroundColor = [UIColor redColr];
cell.img.image = [UIImage imageNamed:#"rahul.png"];
}
PS: there are many other work arounds for this...it depends on how do you want to implement the code.
Other way,
//set flag variable as
#property (strong ,nonatomic) NSIndexPath *selectedIndexPath;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
self.selectedIndexPath = indexPath;
[tableView reloadData];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cells=#"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cells];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cells];
}
strip = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 7, cell.frame.size.height)];
strip.backgroundColor = [UIColor orangeColor];
TitleLbl = [[UILabel alloc] initWithFrame:CGRectMake(65, 7, 130, 35)];
TitleLbl.text =[Right_Menu_array objectAtIndex:indexPath.row];
TitleLbl.textColor=[UIColor darkGrayColor];
TitleLbl.font = [UIFont systemFontOfSize:15];
img = [[UIImageView alloc] initWithFrame:CGRectMake(10, 7, 35, 35)];
img.image = [UIImage imageNamed:[imageArr1 objectAtIndex:indexPath.row]];
if(indexPath == selectedIndexPath)
{
TitleLbl.textColor = [UIColor redColor];
strip.backgroundColor = [UIColor redColr];
img.image = [UIImage imageNamed:#"rahul.png"];
}
[cell.contentView addSubview:strip];
[cell.contentView addSubview:TitleLbl];
[cell.contentView addSubview:img];
}
//This code is not tested.
I suppose you are referring to the tableview header.
You can only set tableview header view to your own custom view using the tableHeaderView property of the tableview. You can set the background color of your custom header view normally.
E.g.
self.tableView.tableHeaderView = myHeaderView;
Alternatively, if you want to modify the section header view, you can use one of the following delegate methods:
tableView:viewForHeaderInSection:
dequeueReusableHeaderFooterViewWithIdentifier:
and return your custom header view there.
From the code, I can see you hold the reference for properties of last known strip, TitleLbl and img, which will always be the components from last UITableViewCell accessed from
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
Since your cell has an orange strip on left side, I would prefer creating a custom UITableViewCell to hold the references.
#interface CustomCell : UITableViewCell
#property (nonatomic,strong) UIView *strip;
#property (nonatomic,strong) UILabel *titleLbl;
#property (nonatomic,strong) UIImageView *imgView;
#end
then with a custom cell, your code can be
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellIdentifier = #"cellIdentifier";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
// create and add components
cell.strip = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 7, cell.frame.size.height)];
[cell.contentView addSubview:cell.strip];
cell.titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(65, 7, 130, 35)];
[cell.contentView addSubview:cell.titleLbl];
cell.imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 7, 35, 35)];
[cell.contentView addSubview:cell.imgView];
}
// modify existing components
cell.strip.backgroundColor = [UIColor orangeColor];
cell.titleLbl.text = [Right_Menu_array objectAtIndex:indexPath.row];
cell.titleLbl.textColor = [UIColor darkGrayColor];
cell.titleLbl.font = [UIFont systemFontOfSize:15];
cell.imgView.image = [UIImage imageNamed:[imageArr1 objectAtIndex:indexPath.row]];
if ([cell isSelected]) {
// remember to keep the changes for cell in it's selected state
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
CustomCell *cell = (CustomCell *) [tableView cellForRowAtIndexPath:indexPath];
cell.titleLbl.textColor = [UIColor redColor];
cell.strip.backgroundColor = [UIColor redColor];
cell.imgView.image = [UIImage imageNamed:#"rahul.png"];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell *) [tableView cellForRowAtIndexPath:indexPath];
cell.titleLbl.textColor = [UIColor darkGrayColor];
cell.strip.backgroundColor = [UIColor whiteColor];
cell.imgView.image = [UIImage imageNamed:#"rahul.png"];
}
also you can override the method in your custom cell
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
I am trying to return the number of objects from myArray and depending on the value, change the headerView in my tableView. The app is functioning in the following way, the user searches for an object, if the object exists the object will appear in the tableView. If the user searches for an object that doesn't exist, nothing will appear in the tableView. Pretty easy..
Eventhough the user searches for an object that doesn't exist it seems like the array stores a value in some-way and the tableView/array is not nil anymore? But nothing is displayed in the tableView. What am I doing wrong? searchResults is the array in my tableView.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.searchResults count];
}
- (UITableViewCell *)tableView:(UITableView *)pTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"SearchCell";
UITableViewCell *cell = [pTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Task *task = [self.searchResults objectAtIndex:indexPath.row];
UILabel *nameLabel = (UILabel *)[cell viewWithTag:1];
UILabel *bookLabel = (UILabel *)[cell viewWithTag:2];
UILabel *chapterLabel = (UILabel *)[cell viewWithTag:3];
[nameLabel setText:task.name];
[bookLabel setText:task.book.name];
[chapterLabel setText:task.chapter.name];
return cell;
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
if (self.searchResults == nil){
headerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"lista2.png"]];
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(5, 3, tableView.bounds.size.width - 5, 18)] autorelease];
label.text = #"Information";
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
[headerView addSubview:label];
}
else if (self.searchResults != nil){
headerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"lista2.png"]];
UILabel *label2 = [[[UILabel alloc] initWithFrame:CGRectMake(5, 3, tableView.bounds.size.width - 5, 18)] autorelease];
label2.text = #"Search results";
label2.textColor = [UIColor whiteColor];
label2.backgroundColor = [UIColor clearColor];
[headerView addSubview:label2];
}
return headerView;
}
So, it sounds like self.searchResults is always non nil.
Replace
if (self.searchResults == nil){
with
if ([self.searchResults count] == 0){
and replace
else if (self.searchResults != nil){
with
else {
1)i want to create a striked text so i have used thet ext frame and i have created it and keep on the lbal it works fine when i have used it in the table cell when i am selecting the cell the strike on the label disaperared
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
#pragma mark TableView delegate method.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView1
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView1 numberOfRowsInSection:(NSInteger)section
{
return 4;
}
- (CGFloat)tableView:(UITableView *)tableView1 heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 120;
}
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
tableView1.backgroundColor = [UIColor clearColor];
cell=nil;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
//cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundColor = [UIColor clearColor];
// cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"strip_11C.png"]];
//cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"strip_11C_h.png"]];
UILabel *price1_lbl = [[UILabel alloc] initWithFrame:CGRectMake(2,80,100,26)];
price1_lbl.text = #"label";
price1_lbl.textColor = [UIColor grayColor];
price1_lbl.font = [UIFont fontWithName:#"Arial-BoldMT" size:(13.0)];
price1_lbl.backgroundColor = [UIColor clearColor];
NSString *string =price1_lbl.text;
CGSize stringSize = [string sizeWithFont:price1_lbl.font];
CGRect buttonFrame = price1_lbl.frame;
CGRect labelFrame = CGRectMake(buttonFrame.origin.x ,
4+buttonFrame.origin.y + stringSize.height/2,
stringSize.width, 1);
UILabel *lineLabel = [[UILabel alloc] initWithFrame:labelFrame];
lineLabel.backgroundColor = [UIColor blackColor];
[cell.contentView addSubview:price1_lbl];
[cell.contentView addSubview:lineLabel];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
secondviewcontroller *obj=[[secondviewcontroller alloc]initWithNibName:#"secondviewcontroller" bundle:nil];
[self.navigationController pushViewController:obj animated:YES];
}
Try to add the strikethrough line directly to the label with the text (price1_lbl). If that doesn't work you may want to give this lib a try.
UPDATE:
You can use this code:
CGSize size = [price1_lbl.text sizeWithFont:[UIFont systemFontOfSize:16.0f]];
UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0 , label.frame.size.height / 2, size.width, 1)];
line.backgroundColor = [UIColor blackColor];
[price1_lbl addSubview:line];