UIView background color does not take effect - objective-c

Can someone tell me why this is not working right?
I have these lines of code below within my table view cell for tableView:didSelectAtIndexRowPath: method.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[[tableView viewWithTag:199]removeFromSuperview];
CGSize cellSize = [tableView cellForRowAtIndexPath:indexPath].frame.size;
UIView *subSelectionView = [[UIView alloc]initWithFrame:CGRectMake(10, 0, (int)cellSize.width - 20, 100)];
[subSelectionView setBackgroundColor:[UIColor blueColor]];
subSelectionView.layer.borderColor = [UIColor grayColor].CGColor;
subSelectionView.layer.borderWidth = 1;
subSelectionView.tag = 199;
[[tableView cellForRowAtIndexPath:indexPath]addSubview:subSelectionView];
}
Notice the code:
[subSelectionView setBackgroundColor:[UIColor blueColor]];
Obviously, I want to change the color of the subview that I added to the UITableViewCell but why is it not working ?

jus reload the table after adding the subview on the table
[[tableView cellForRowAtIndexPath:indexPath]addSubview:subSelectionView];
[tableview reloadData];

Add the following line to the start of the method
[tableView deselectRowAtIndexPath:indexPath animated:NO];
So ur method would be
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
[[tableView viewWithTag:199]removeFromSuperview];
....
}

Related

UIView addSubview nor working in UITableViewCell

barView I have a Table View and, in each cell, I want to draw some statistic bars. I have an empty UIView created in my custom cell, its name is mainView and it is the view where the chart is drawn.
In cellForRowAtIndexPath I'm doing this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CustomCellIdentifier";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
CGRect barRect = CGRectMake(0, 0, 100, 10);
UIView *barView = [[UIView alloc] initWithFrame:barRect];
[barView setBackgroundColor:[UIColor redColor]];
[cell.mainView addSubview:barView];
return cell;
}
The barView is not displaying on loading. It only shows after I change and return from another tab or after the cell goes out of the screen.
Notes:
I have tried [cell.mainView setNeedsDisplay];
I have a header made with another custom cell.
Try this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CustomCellIdentifier";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
CGRect barRect = CGRectMake(0, 0, 100, 10);
UIView *barView = [[UIView alloc] initWithFrame:barRect];
[barView setBackgroundColor:[UIColor redColor]];
[cell addSubview:barView]; // use cell.mainView if you have that created somewhere other than here (like IB)
}
return cell;
}
dequeueResuable... can return a nil cell. So you need to have a check, to see if you should alloc/init a new cell. In the code I gave you if dequeueResuable... returns you a proper cell then it would have been created with the subview intact already. So no need to re-add it (hence creating it only in if (!cell))
Your description clearly indicates you are filling your barView outside this function. So that it becomes visible only after transition.
Or you are simply overwriting it (or even overwriting mainview) outside what you have provided here.
Check your viewdidload and viewdidappear and the likes.
I have solved it easily with:
- (void)viewDidAppear:(BOOL)animated
{
[self.tableView reloadData];
}
It seems that adding a subview cant be done before the view is created in the window, before the cells are completely loaded.

How to add a content view on UITAbleViewCell when a row is selected?

I'm trying to add a button when a row is selected in UITableView and also remove it when row is tapped second time, i don't want custom UITableViewCell.
Any suggestion or sample code will be appreciated.
code i've tried:
in cellForRowAtIndexPath method
if(cell.selected == YES){
UITextField* numOfBottles =[[UITextField alloc] initWithFrame:CGRectMake(240,9.0f,50, 25)];
numOfBottles.tag = indexPath.row;
[numOfBottles setBorderStyle:UITextBorderStyleNone];
[numOfBottles setBackgroundColor:[UIColor clearColor]];
[numOfBottles setTextColor:[UIColor whiteColor]];
[numOfBottles setTextAlignment:UITextAlignmentLeft];
[numOfBottles setBackground:[UIImage imageNamed:#"blue_dropdown_normal.png"]];
[numOfBottles setFont:[UIFont systemFontOfSize:16.0f]];
[numOfBottles setDelegate:self];
NSString* quantity = [[NSString alloc] initWithString:[subtotalObj.qtyArray objectAtIndex:(indexPath.row - 1)]];
[numOfBottles setText:quantity];
[numOfBottles setTextAlignment:UITextAlignmentCenter];
[numOfBottles setBackgroundColor:[UIColor whiteColor]];
numOfBottles.keyboardType = UIKeyboardTypeDefault;
// numOfBottles.tag = indexPath.row;
[cell.contentView addSubview:numOfBottles];
[numOfBottles release];
}
and in didSelectRowAtIndexPath method
[tableView reloadData];
But still button(textfield with background image) is not rendered.
I think the best way is to use a UITableViewCell subclass.
But you can use my answer on another question to make your TableView rerender and place a button in your cell.
- (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];
}
if(indexPath.row == selectedRow){ // if your criteria where met
//add your button
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//do your select things here
selectedRow = indexPath.row; //where selectedRow is a class variable
[self.tableView reloadData]; // rerenders the tableview
}
There's UITableView delegate, - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath implement it, where you'll get the row which user tapped, get the current cellusing cellForRowAtIndexPath: you need to pass NSIndexPath using the section and row. Now check if cell.contentView subviews has UIButton which you want? If yes, perform removeFromSuperView else addSubView. If you cell have already filled with UIButtons you can give unique tag to distinguish them.
Use these delegates
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
//Create reference of button with tag 999
cell.contentView addSubView:yourButtonReference]
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
UIButton *btnAdded = (UIButton *)[cell viewWithTag:999];
if(btnAdded){
[btnAdded removeFromSuperView];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
}

Looping through subview of a custom cell in didSelectRowAtIndexPath to get UITextField text

i know im asking a very common question. but after going through a lots of similar questions and articles im not able understand
How can i access UITextfield inside UITableViewCell inside didSelectRowAtIndexPath delegate method.
im posting my code below. i may b not going with expected way although..
i want to make textfield (of selected row) [becomeFirstResponder] in didSelectRowAtIndexPath
so that i can display uiPicker below to this textfield. Please help me out with this.
- (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];
if(self.isTableForHospital)
{
UIView *myview=[self createRowViewForHospitalTable:[customArray objectAtIndex:indexPath.row]];
myview.tag=indexPath.row;
[cell addSubview:myview];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
else
{
cell.textLabel.text=[customArray objectAtIndex:indexPath.row];
}
}
return cell;
}
-(UIView *)createRowViewForHospitalTable:(NSString *)rowText
{
UIView *hospital_row_view=[[UIView alloc]init];
UILabel *lblRowText=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 50)];
lblRowText.text=rowText;
txtDepartment=[[UITextField alloc]initWithFrame:CGRectMake(310, 0, 185, 30)];
//...rest styling for textfield goes here..
txtRole=[[UITextField alloc]initWithFrame:CGRectMake(495, 0, 185, 30)];
[hospital_row_view addSubview:lblRowText];
[hospital_row_view addSubview:txtDepartment];
[hospital_row_view addSubview:txtRole];
return hospital_row_view;
}
- (void)textFieldDidEndEditing:(UITextField *)textField { }
- (void)textFieldFinished:(id)sender{
NSString *value=#"sa";
[sender resignFirstResponder];
}
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cellSelected = [tableView cellForRowAtIndexPath: indexPath];
UIView *myview=[cellSelected.contentView viewWithTag:indexPath.row];
for(UIView *item in [myview subviews])
{
if([item isKindOfClass:[UITextField class]])
{
[item becomeFirstResponder];
}
}
}
In terms of iterating through the cell subviews and getting the UITextField, I think I have your answer.
I found this on another thread here, and modified it to be more generic:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITextField* textField = [[UITextField alloc] init];
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
UIView* subview = [[cell.contentView subviews] lastObject]; // Make sure your UITextField really *is* the last object you added.
if ([subview isKindOfClass:[UITextField class]]) {
textField = (UITextField*)subview;
}
[textField becomeFirstResponder];
}
...where "textField" is the placeholder for your actual text field object.
Hope this helps!
I don't think you need to alloc init the textfield, for it will ultimately be pointing to the cell's textfield (if any).
I believe you are allocating it unnecessarily.

Activate cell's accessory view (UISwitch) when didSelectRowAtIndexPath is called

Every cell of my tableView has a UISwitch as an accessoryView:
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = mySwitch;
[mySwitch addTarget:self action:#selector(SwitchToggle:) forControlEvents:UIControlEventValueChanged];
Switch is toggled when the user taps it, but I also want it toggled when the didSelectRowAtIndexPath is called (when the user taps on that cell).
I tried this (but it doesn't work):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UISwitch *tempSwitch = (UISwitch *)[tableView cellForRowAtIndexPath:indexPath].accessoryView;
[tempSwitch addTarget:self action:#selector(SwitchToggle:) forControlEvents:UIControlEventValueChanged];
[tableView deselectRowAtIndexPath:indexPath animated:YES]; // cell selection fadeout animation
}
Thanks.
The code must look like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UISwitch *tempSwitch = (UISwitch *)[tableView cellForRowAtIndexPath:indexPath].accessoryView;
[tempSwitch setOn:!tempSwitch.on animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Remember that you also have to update your model.

UITextField unable to become first responder if added to UITableViewCell via accessoryView

Like many other users, I'm trying to set throw a UITextField into a UITableViewCell to be used for editing a row. But instead of adding a new view, I'm trying to use the accessoryView property. At first I tried this...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITextField *textField =[[UITextField alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 44.0f)];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryView = textField;
[textField becomeFirstResponder];
}
...and the UITextField is added properly, but I need to tap it again to get the keyboard to show. However if I do this...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITextField *textField =[[UITextField alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 44.0f)];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell addSubview:textField];
[textField becomeFirstResponder];
}
...it works, the keyboard shows up but, I don't get any of the other benefits of having it as the accessoryView (easier positioning with other views moving around it). I suppose the addSubview: call is altering the responder chain or something and allowing my UITextField to become the first responder, but thought maybe there was another way to do this allowing me to set the cell's accessoryView instead. Thanks.
I hate my solution, but it works and am up for a better way.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
CGRect detailTextLabelFrame = cell.detailTextLabel.frame;
detailTextLabelFrame.origin.x -= 100.0f;
detailTextLabelFrame.size.width += 100.0f;
UITextField *textField = [[UITextField alloc] initWithFrame:detailTextLabelFrame];
cell.detailTextLabel.hidden = YES;
cell.accessoryView = textField;
// These are the two lines that made it work, but it feels ugly
[cell.accessoryView removeFromSuperview];
[cell addSubview:cell.accessoryView];
[cell.accessoryView becomeFirstResponder];
}
I hope you know about textfield delegate.Try this way....may be it will help you.
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
if(theTextField == yourtextfieldname) {
[yourtextfieldname resignFirstResponder];
}
return YES;
}