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;
Related
I am trying to customize my tableView in my IOS app. When my tableView(or rather array) is empty, I want to display a customized label instead of the items in the tableView. The label I am referring to is "label0". But something is terribly wrong, my [label0 setHidden:YES]; or [label0 setHidden:NO]; only works in the first block of the if "method"? In the second block (if else) nothing happens no matter what I try to set the label as (hidden or shown).
What have I missed? I cannot see my own fault?
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
UILabel *label0 = [[[UILabel alloc] initWithFrame:CGRectMake(0, 25, tableView.bounds.size.width - 0, 100)] autorelease];
if ([self.searchResults count] == 0){
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];
label0.text = #"Test test test";
label0.textColor = [UIColor blackColor];
label0.backgroundColor = [UIColor whiteColor];
[tableView addSubview:label0];
[label0 setHidden:NO];
}
else {
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];
[label0 setHidden:YES];
}
return headerView;
}
EDIT
I have moved the code to viewDidLoad and set the property for the UILabel. This have unfortunately not solved my problem....
UILabel *label0 = [[[UILabel alloc] initWithFrame:CGRectMake(0, 25, tableView.bounds.size.width - 0, 100)] autorelease];
[tableView addSubview:label0];
if ([self.searchResults count] == 0){
label0.text = #"Test test test";
label0.textColor = [UIColor blackColor];
label0.backgroundColor = [UIColor whiteColor];
[label0 setHidden:NO];
}
else {
[label0 setHidden:YES];
}
This is because your label0 is created every time this method is called so in "else" you are referring to totally different object (not the one that you added to tableView when array was empty).
You shouldn't be adding subviews to tableView from this method. Consider using viewDidLoad. That way you will be adding label0 only once. To achieve that add label0 as property of your viewController.
You forgot to add label0 as subview please put this line in the else statement
[tableView addSubview:label0];
also I cannot see any benefit from doing so. I believe you can just hide the table view and show another view that has the label. but nesting views that way is not good when you come back to debug this code after 1 month you will struggle to understand it.
You say in your edit that you set the property for the UILabel (I assume you mean that you have a property called label0?). If this is so, then when you alloc init your label, it should be self.label0 = ..... not UILabel *label0 = .....
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.
I want to reload the data from uitableview when I press the button.
example:
if I press button 1, then I will present data with id 1, and if I press the button 2 then I will present data with id 2 and previous data are not shown.
Anyone know how, I use [tableview reloadData]; not working, change happens if I move the screen and return to the screen that displays the data in the tableview?
Thank's before.
Follow This link, perhaps it will help you.
stackoverflow.com/questions/7290340/xcode-tableview-showing-to-different-arrays-one-at-a-time-problem
Use this code.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.tableView reloadData];
}
if fired when u click the button simply write this also
-(IBAaction) buttonclick
{
[self.tableView reloadData];
// your code...
}
You should not create UILabel *labelMessage; and other variables each time your application calls tableView:cellForRowAtIndexPath:. Instead try to do this with all your variables:
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UILabel *labelMessage;
labelMessage = [[UILabel alloc] initWithFrame:CGRectZero];
labelMessage.backgroundColor = [UIColor clearColor];
labelMessage.tag = 5;
labelMessage.numberOfLines = 0;
labelMessage.lineBreakMode = UILineBreakModeWordWrap;
labelMessage.font = [UIFont systemFontOfSize:14.0];
labelMessage.shadowOffset = CGSizeMake(0, 1);
labelMessage.shadowColor = [UIColor whiteColor];
labelMessage.backgroundColor = [UIColor clearColor];
labelMessage.textColor = [UIColor colorWithRed:136.00/255.00 green:91.00/255.00 blue:14.00/255.00 alpha:1.0];
UIView *message = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, cell.frame.size.width, cell.frame.size.height)];
[message addSubview:labelMessage];
}
UILabel *labelMessage = (UILabel *)[cell viewWithTag:3];
/* Set textMessage value here */
labelMessage.text = textMessage;
return cell;
I have the following code which draws a separator line and text for a UITableViewCell. It looks fine but when I scroll off screen then back, the separator line is gone but the text is still fine. Any ideas?
static NSString *aProgressIdentifier = #"CustomerCell";
UITableViewCell *aCustomerCell = [iTableView dequeueReusableCellWithIdentifier:aProgressIdentifier];
if (!aCustomerCell) {
aCustomerCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:aProgressIdentifier] autorelease];
aCustomerCell.contentView.backgroundColor = [UIColor whiteColor];
UIImageView *aLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 72, 800, 1)];
aLine.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
[aCustomerCell addSubview:aLine];
[aLine release];
}
CMACustomer *aCustomerObject = aCellObject;
aCustomerCell.textLabel.text = aCustomerObject.customerFullName;
aCustomerCell.detailTextLabel.text = nil;
aCell = aCustomerCell;
Try to add the "aLine" image view as subview of the contentView and not the whole table itself. Probably when the cell is reused and then layoutSubviews is called again, the contentView overlaps (white background) your aLine. Infact consider that iOS default cells have their subviews dynamically redrawn and resized each time they are displayed on screen.
So I would try this:
[aCustomerCell.contentView addSubview:aLine];
If this doesn't work, what can you do is to remove the contentView completely and add your own custom subviews (do this inside the if(!aCustomerCell) and not outside unless you will not get the benefits of the cell re-use):
if (!aCustomerCell) {
aCustomerCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:aProgressIdentifier] autorelease];
[cell.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
UIImageView *aLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 72, 800, 1)];
aLine.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
[aCustomerCell.contentView addSubview:aLine];
[aLine release];
}
Finally another check is verify that the cell height is > 72 (it seems a trivial check but its often source of headaches!).
The table view is using a pool of cells, so you can't be sure which one you're getting for any given index path. You can use the cell or the content view, but be sure to add only one of your custom lines per cell.
UIImageView *aLine = (UIImageView *)[cell viewWithTag:64];
if (!aLine) {
// etc.
UIImageView *aLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 72, 800, 1)];
aLine.tag = 64;
[cell addSubview:aLine];
//
}
// other formatting logic here, you can also hide/show aLine based on biz logic
try adding it to the contentView
[aCustomerCell.contentView addSubview:aLine]
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.