UITableView reload data when click button - iphone

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;

Related

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;

UILabel sizeToFit not retained when back button pressed to go back to rootViewController

So my rootViewController contains a UICollectionView with a Custom UICollectionViewCell. I have a label on the cell for a header which shouldn't ever change and I have a multi-line Label making up the body of the rest of the cell. When I launch the app it calls the sizeToFit correctly and top aligns everything so it isn't centered. I click on one of the cells and go to the next view, then if I click the back button to go back to the rootViewController it does run the viewWillAppear method and reloads the data but the sizeToFit does not work and everything on the multi-line label becomes center aligned. It is still multiple lines but if there is only a couple of lines it sits in the center of the label and it doesn't look good. How can I keep this so it is consistant. I have a left menu that will reload the rootViewController which will position the label correctly again but once I hit the back button from the secondViewController it is no longer aligned. Is there a way to make it clear all of the cells and reload them. I have tried the [collectionView reloadData]; in the viewWillAppear and it doesn't work, it is currently called at the end of the connectionDidFinish which the network connection is called from the viewWillAppear method. Any assistance is appreciated.
This is the custom UICollectionViewCell code
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
[contentLabel setNumberOfLines:0];
[contentLabel sizeToFit];
}
RootViewController
-(UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:
(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"LocationListCell";
LocationList *cell = [cv dequeueReusableCellWithReuseIdentifier:cellIdentifier
forIndexPath:indexPath];
[cell.titleBar setText:[value objectAtIndex:indexPath.row]];
NSArray *tempObject = [[NSArray alloc] initWithArray:[self getData:[key
objectAtIndex:indexPath.row]]];
NSMutableString *tempString = [[NSMutableString alloc] init];
for (int i = 0; i < [tempObject count]; i++)
{
[tempString appendString:[tempObject objectAtIndex:i]];
[tempString appendString:#"\r"];
}
//[cell.contentLabel sizeToFit];
//[cell.contentLabel setNumberOfLines:0];
[cell.contentLabel setText:tempString];
return cell;
}
Use this dynamic UILable with this custom method...
just add this bellow method in your .m file
-(float) calculateHeightOfTextFromWidth:(NSString*) text: (UIFont*)withFont: (float)width :(UILineBreakMode)lineBreakMode
{
[text retain];
[withFont retain];
CGSize suggestedSize = [text sizeWithFont:withFont constrainedToSize:CGSizeMake(width, FLT_MAX) lineBreakMode:lineBreakMode];
[text release];
[withFont release];
return suggestedSize.height;
}
and use it like bellow..
UILabel *yourLable = [[UILabel alloc]init];
[yourLable setFrame:CGRectMake(110, 31, 200, 50)];
[yourLable setText:tempString];
yourLable.lineBreakMode = UILineBreakModeWordWrap;
yourLable.numberOfLines = 0;
yourLable.font = [UIFont fontWithName:#"Helvetica" size:12];
yourLable.frame = CGRectMake(yourLable.frame.origin.x, yourLable.frame.origin.y,
200,[self calculateHeightOfTextFromWidth:yourLable.text :yourLable.font :200 :UILineBreakModeWordWrap] );
yourLable.textColor = [UIColor darkGrayColor];
cell.contentLabel = yourLable;
hope this help you...
Try these lines:
contentLabel.textAlignment = UITextAlignmentLeft;
contentLabel.lineBreakMode = UILineBreakModeWordWrap;
Try like that and declare label in .h file
contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 49, 202, 237)];
[contentLabel setNumberOfLines:0];
[contentLabel sizeToFit];
[cell.contentLabel setText:tempString];

Label refuses to disappear with [label setHidden:YES]; command in a if-method?

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 = .....

Help debugging UITableView, shows no data

I was first tasked to create a popover that came from a BarButtonItem, and then based on selection in that popover (which was a tableview), another popover would present itself from the cell with the data. The data I had was correctly presented that way. In the debugger, I still see the data in my cellForRowAtIndexPath with NSLog what's in the self.CategoriesArray. For some reason though, the data will not show... Now however, they don't want the initial popover, and just one popover that comes from the BarButtonItem. For the life of me, I cannot figure out why my data is not being presented since all that change should be is replacing the first UITableView in the popover, with the second UITableView. Unless I'm missing something..... Any help would be appreciated. Thanks!
cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == _filterTableView || tableView == _categoriesTableView) {
static NSString *simpleIdentifier = #"SimpleIdentifier";
UITableViewCell *simpleCell = [tableView dequeueReusableCellWithIdentifier:simpleIdentifier];
if (simpleCell == nil) {
simpleCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleIdentifier];
}
NSUInteger row;
row = [indexPath row];
simpleCell.textLabel.textColor = [UIColor whiteColor];
simpleCell.selectionStyle = UITableViewCellSelectionStyleNone;
// first popover
if (tableView == _filterTableView) {
simpleCell.textLabel.text = [_filterArray objectAtIndex:row];
return simpleCell;
}
// second popover
else if (tableView == _categoriesTableView) {
simpleCell.textLabel.text = [_categoriesArray objectAtIndex:row];
return simpleCell;
}
- (IBAction)FilterButtonPressed:(id)sender {
// This part works for two popovers
// UIViewController *contentViewController = [[UIViewController alloc] init];
// self.FilterTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 200, 150) style:UITableViewStylePlain];
// self.FilterTableView.delegate = self;
// self.FilterTableView.dataSource = self;
// self.FilterTableView.bounces = NO;
// self.FilterTableView.scrollEnabled = NO;
// self.FilterTableView.backgroundColor = [UIColor clearColor];
// contentViewController.contentSizeForViewInPopover = CGSizeMake(200, 150);
// contentViewController.view = _filterTableView;
//
// self.FilterPopoverController = [[UIPopoverController alloc] initWithContentViewController:contentViewController];
// [self.FilterPopoverController presentPopoverFromBarButtonItem:_filterButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
//
// [contentViewController release];
// New code tfor one popover
[self loadCategories];
UIViewController *contentViewController = [[UIViewController alloc] init];
self.CategoriesTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 500) style:UITableViewStylePlain];
self.CategoriesTableView.delegate = self;
self.CategoriesTableView.dataSource = self;
self.CategoriesTableView.bounces = NO;
self.CategoriesTableView.scrollEnabled = YES;
self.CategoriesTableView.backgroundColor = [UIColor clearColor];
contentViewController.contentSizeForViewInPopover = CGSizeMake(320, 500);
contentViewController.view = _categoriesTableView;
self.FilterPopoverController = [[UIPopoverController alloc] initWithContentViewController:contentViewController];
self.FilterPopoverController.delegate = self;
[self.FilterPopoverController presentPopoverFromBarButtonItem:_filterButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[contentViewController release];
}
}
I recognize this code from your other question. :) You're setting your text color to white, and the default background color of the cell is also white. Is this the same bug? Try setting a different background color for your cell.
If that doesn't fix it, there's one other thing you could check. You're setting this:
contentViewController.view = _categoriesTableView;
but it's not clear from your code where _categoriesTableView comes from. Do you have #synthesize CategoriesTableView = _categoriesTableView; at the top of your implementation?
Set a breakpoint on that view assignment and make sure _categoriesTableView isn't nil.

How to customize tableView Section View - iPhone

I know how to customize the tableViewCell.
I have seen many application customizing the tableView Cell.
Similarly, I want to customize TableView Section Header
"Suppose - A section name should be in different font, It has different background images etc."
Is it possible How?
In which method Should I implement the code?
Instead of using the normal method
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
You want to implement this one:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
As you can see, the second one returns a UIView instead of just string for text. Therefore you can customise your own view (with labels etc) and return it.
Here is an example of how you might do that (to be implemented in the above method):
// create the parent view that will hold header Label
UIView* customView = [[[UIView alloc] initWithFrame:CGRectMake(10,0,300,60)] autorelease];
// create image object
UIImage *myImage = [UIImage imageNamed:#"someimage.png"];;
// create the label objects
UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.font = [UIFont boldSystemFontOfSize:18];
headerLabel.frame = CGRectMake(70,18,200,20);
headerLabel.text = #"Some Text";
headerLabel.textColor = [UIColor redColor];
UILabel *detailLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
detailLabel.backgroundColor = [UIColor clearColor];
detailLabel.textColor = [UIColor darkGrayColor];
detailLabel.text = #"Some detail text";
detailLabel.font = [UIFont systemFontOfSize:12];
detailLabel.frame = CGRectMake(70,33,230,25);
// create the imageView with the image in it
UIImageView *imageView = [[[UIImageView alloc] initWithImage:myImage] autorelease];
imageView.frame = CGRectMake(10,10,50,50);
[customView addSubview:imageView];
[customView addSubview:headerLabel];
[customView addSubview:detailLabel];
return customView;