UITableView add subview - iphone

I want to add subview in UITableView
I tried like this way:
[self.tableView addSubView:myView.view];
I didn't got any error but view is not getting added, can anyone please help..?
Thanks.

Try this:
view.frame = CGRectMake(view.frame.origin.x, -view.frame.size.height, view.frame.size.width, view.frame.size.height);
[self.tableView addSubview:view];
[self.tableView setContentInset:UIEdgeInsetsMake(view.frame.size.height, 0, 0, 0)];
this will add the view above the cells and move the cells down by the height of the added view.

I am no getting why you are adding subview to Tableview. May be you want to add something to each cell of tableView. You can do this by adding:
[cell.contentView addSubview:yourSubView];
in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
}

Both of self.tableView and myView should not be nil
What do you mean myView.view?
this piece of code works good for me:
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(20, 80, 200, 20)];
labelView.text = #"TEST";
[headerView addSubview:labelView];
[self.tableView addSubview:headerView];

For Swift:
let foregroundView = UILabel(frame: CGRectMake(0, 0, tableView.bounds.size.width, tableView.bounds.size.height))
foregroundView.backgroundColor = UIColor.whiteColor()
tableView.addSubview(foregroundView)
foregroundView.bringSubviewToFront(tableView)
You can remove it also:
foregroundView.removeFromSuperview()

Simply replace your line of code with the one below:
[self.tableView.superview addSubview:myView];
Note:
I am not certain about myView.view in line of code you mentioned in your question.

Related

Button is not responsive on headerview of UITableView

I am creating headers for my tableview.And adding view on that header.
In that view there are two imageviews and a button
The button is not responsive(a very small area on the top part is responsive)
Why this is happening,Please help me ?
I have tried with some SO answers- like "setAutoresizingMask:UIViewAutoresizingNone"
But nothing is working
Here is my code
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// ADDING A VIEW
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 310, 39.0)];
header.backgroundColor = [UIColor clearColor];
header.userInteractionEnabled = YES;
[header setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
// ADDING IMAGE VIEW
UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 310, 39.0)];
image.image = [UIImage imageNamed:#"headertop.png"];
[image setUserInteractionEnabled:TRUE];
[header addSubview:image];
// ADDING ANOTHER IMAGEVIEW (beneath the view's frame)
UIImageView *downImage = [[UIImageView alloc]initWithFrame:CGRectMake(0 , 39, 310, 23)];
downImage.image = [UIImage imageNamed:#"headerbottom.png"];
[header addSubview:downImage];
// ADDING BUTTON
UIButton *placeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[placeBtn addTarget:self
action:#selector(goToRestPage:)
forControlEvents:UIControlEventTouchUpInside];
placeBtn.tag = section;
placeBtn.backgroundColor = [UIColor yellowColor];
placeBtn.frame = CGRectMake(75,20, 150, 20);
[header addSubview:placeBtn];
return header;
}
Use this Method. I think you miss this method.
-(float)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30.0;
}
Or this method already exists so increase the height of the header.
-(float)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 80.0;
}
Please do this :
User Interaction Enabled to TRUE from your XIB. by default its FALSE.
Thanks,

Overlapping UITextView when add text to UITextView

I created UIViewController in Storyboard and added tableview as a Subview. Its working fine. In that tableview I added cells programatically and each cell contains UITextFields or UITextView. UITextFields are working fine. But UITextViews have some troubles. EX: When I add text to UITextView then bellow UITextViews are overlapped. why is that ? I tried to sort out whole day. But still I couldn't. :(
case cellResponseNote:
cell = [tableView.tableView dequeueReusableCellWithIdentifier:CellIdentifierResponseNote];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierResponseNote];
cell.frame = CGRectMake(0, 0, cell.frame.size.width, cellNotesHeight);
//cell.frame = CGRectMake(0, 0, cell.frame.size.width, cellNotesHeight);
if (!self.txtVwResponseNote) {
// self.txtVwResponseNote = [[UITextView alloc] initWithFrame:cell.frame];
self.txtVwResponseNote = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cellNotesHeight)];
[self.txtVwResponseNote setScrollEnabled:NO];
[self.txtVwResponseNote setAutoresizingMask:UIViewAutoresizingNone];
[self.txtVwResponseNote setDelegate:self];
[self.txtVwResponseNote setFont:[UIFont systemFontOfSize:14]];
[self.txtVwResponseNote setInputAccessoryView:self.keyboardToolbar];
[self setupMailData:indexPath.row];
}
}
self.txtVwResponseNote.editable = !(self.isMailForReview);
// [cell.contentView addSubview:self.txtVwResponseNote];
[cell addSubview:self.txtVwResponseNote];
contentSubview = self.txtVwResponseNote;
In this image bottom You can see three cells. ( bellow Footer) When I add text value for top text view. Then overlap the next text view.
Bellow image you can see only two textviews. Bottom text view is not showing.
So what should I do ?
First of all as an answer try setting the height of the cell by following method.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
case cellResponseNote:
return cellNotesHeight;
default:
return defaultHeight;
}
Also I recommend you not to share the textview property. Instead try modifying your code like this and see whether your code works.
case cellResponseNote:
cell = [tableView.tableView dequeueReusableCellWithIdentifier:CellIdentifierResponseNote];
UITextView *txtVwResponseNote = nil;
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierResponseNote];
cell.frame = CGRectMake(0, 0, cell.frame.size.width, cellNotesHeight);
//cell.frame = CGRectMake(0, 0, cell.frame.size.width, cellNotesHeight);
//if (!self.txtVwResponseNote) {
// self.txtVwResponseNote = [[UITextView alloc] initWithFrame:cell.frame];
txtVwResponseNote = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cellNotesHeight)];
[txtVwResponseNote setScrollEnabled:NO];
[txtVwResponseNote setAutoresizingMask:UIViewAutoresizingNone];
[txtVwResponseNote setDelegate:self];
[txtVwResponseNote setFont:[UIFont systemFontOfSize:14]];
[txtVwResponseNote setInputAccessoryView:self.keyboardToolbar];
[txtVwResponseNote setTag:100];
//}
} else {
txtVwResponseNote = (UITextView *)[cell viewWithTag:100]
}
[self setupMailData:indexPath.row];
txtVwResponseNote.editable = !(self.isMailForReview);
// [cell.contentView addSubview:txtVwResponseNote];
[cell addSubview:self.txtVwResponseNote];
contentSubview = self.txtVwResponseNote;
It gets repeated because you added it to the cell as a subview, and then reuse the cell. Add it as a accessoryView of the cell instead, and set it to nil for the cell's that dont need it.
cell.accessoryView = self.txtVwResponseNote;
And in all other cells, set accessoryView to nil.
cell.accessoryView = nil;

UITableView headerview keeps repeating

I am making an iPhone app. In this app I have a UITableView which has a UITableView header. This is how my code looks like.
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(30, 0, 308, 50)];
view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"sx_home_tablehead.png"]];
self.tableViewAppointments.tableHeaderView = view;
And this is how it looks like on my iPhone
And this is what I want to achieve
Hope that anyone can help me !
Kind regards !
Because you're specifying explicitly the frame of the view. You should not do a terrible hack like this. Use this piece of code instead:
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"header.png"]];
self.tableView.tableHeaderView = imgView;
If you want to add it as a table view section header, you need to add it as follows,
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(30, 0, 308, 50)];
view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"sx_home_tablehead.png"]];
//or,
UIImageView *view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"sx_home_tablehead.png"]];
return view;
}
You can define the height for header in
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return height;
}

Two TextFields in tableview, all on scrollview, disappear when keyboard comes up iPhone

In My Login View I Have E-mail and Password textfields in a grouped tableview with two cells. I added the two textfields programmatically:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
_cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
_cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:#"Cell"];
_cell.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
if (indexPath.row == 0) {
_emailTxtFld = [[UITextField alloc]initWithFrame:CGRectMake(10, 7, 277, 34)];
_emailTxtFld.placeholder = #"E-mail";
_emailTxtFld.font = [UIFont fontWithName:#"Helvetica-Bold" size:15];
_emailTxtFld.clearsOnBeginEditing = YES;
[_emailTxtFld setDelegate:self];
[_emailTxtFld setKeyboardType:UIKeyboardTypeEmailAddress];
[_cell.contentView addSubview:_emailTxtFld];
}
if (indexPath.row == 1) {
_passwordTxtFld = [[UITextField alloc]initWithFrame:CGRectMake(10, 7, 277, 34)];
_passwordTxtFld.placeholder = #"Password";
_passwordTxtFld.font = [UIFont fontWithName:#"Helvetica-Bold" size:15];
_passwordTxtFld.secureTextEntry = YES;
[_passwordTxtFld setDelegate:self];
_passwordTxtFld.clearsOnBeginEditing = YES;
[_cell.contentView addSubview:_passwordTxtFld];
}
return _cell;
}
This is all on a scrollview. When a textfield is touched, the keyboard comes up, and the the scrollview comes up with the rest of the view being viewable by scrolling. The problem is the textfields and the table view and the text that I type disappear, when i touch inside them to edit them. I can still see the autocorrect, and after I've typed something and the keyboard has returned everything is back to normal and I can see the text I've entered.
If I comment out everything in my keyboardDidShow Method, then the table view and textfields do not disappear. But obviously I want to keep that line in.
-(void)keyboardDidShow:(NSNotification *)notif
{
_logScrollView.frame = CGRectMake(0, 0, 320, 245);// This line commented out stops the problem
}
-(void)keyboardDidHide:(NSNotification *)notif
{
_logScrollView.frame = CGRectMake(0, 0, 320, 460);
}
Let me know if I need to give you some more code.
Thank you for your answers!
Do you have a tableview inside the scrollview? UITableView is already a child class of UIScrollView. You should not have one inside the other.
Another thing: you have this:
_emailTxtFld.clearsOnBeginEditing = YES;
and
_passwordTxtFld.clearsOnBeginEditing = YES;
Which means that they will clear once the textfield becomes the first responder. I dont really understand what is happening after the keyboard dismisses. Can you give a little detail about what is disappearing?
I found a way to solve my problem. I don't know the right wording, but I just created a variable from my tableview that I had already created in IB and in the keyboardDidShow method I made the frame stay where it was like this:
-(void)keyboardDidShow:(NSNotification *)notif
{
_logScrollView.frame = CGRectMake(0, 0, 320, 245);
_tableViewWTxtFlds.frame = CGRectMake(8, 153, 304, 79);
}
-(void)keyboardDidHide:(NSNotification *)notif
{
_logScrollView.frame = CGRectMake(0, 0, 320, 460);
_tableViewWTxtFlds.frame = CGRectMake(8, 153, 304, 79);
}
Apparently the tableview was moving out of my view, that's what I'm guessing was wrong if this fixes it. Thanks for checking out my question.

Different first row for a UITableView

I need a UITableView whose first row should show a logo, while other rows should show the other table datas.
I tried the following code:
if(indexPath.row == 0) {
//Code for imageview
} else {
//Code to display array content
}
My problem is that I don't get the first array item in the table view.
Edited::
if (indexPath.row == 0) {
CGRect myImageRect = CGRectMake(120.0f, 5.0f, 70.0f, 55.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:#"logo.png"]];
[cell.contentView addSubview:myImage];
}
else {
NSDictionary *dict = [menuitems objectAtIndex:indexPath.row];
cell.textLabel.text =[dict objectForKey:#"category"];
}
Try to use an headerview. Use the following code to place logo at the top of row.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
UIImageView *myimgvw = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
[myimgvw setImage:[UIImage imageNamed:#"logo.png"]];
[myView addSubview:myimgvw];
return myView;
}
You haven't shown your code that displays the array content, but I guess you are using something like
[dataArray objectAtIndex:indexPath.row];
But because you are showing the image at row 0, you need to offset this by -1. In other words, the second row in your table will have indexPath.row = 1, but you need that to be objectAtIndex:0.
Then you need to put use indexPath-1 to show text instead of indexPath in else part of above code snippet.