I have a table view where is showed the name of every element of a database. One field is the price of that element.
I use this UILabel to show the sum of all the prices, and it works perfectly.
- (void)viewWillAppear:(BOOL)animated
{
conto = [[NSNumber alloc] initWithDouble:0];
shoppingListItems = [[NSMutableArray alloc] init];
[super viewWillAppear:animated];
[self loadDataFromDb];
[self sortListArray];
[self.tableView reloadData];
if ([conto intValue] < 0) {
walletLabel.textColor = [UIColor redColor];
} else { walletLabel.textColor = [UIColor greenColor]; }
walletLabel.text = [[NSString alloc] initWithFormat: #"Saldo: %#€", [conto stringValue]];
}
"conto" variable is calculate inside "loadDataFromDB" method.
I would like to update it every time I delete a row from the table.
Any suggestion?
Easy. Just call your update routine in your tableView:commitEditingStyle:forRowAtIndexPath: method.
Related
I am building an app in Xcode 5, and I ran into some strange behavior of UISegmentedControl.
first some info on the app i'm building:
I'm building an app in which I want to allow users to order products at registered companies. As an extra service, I want to allow them to see the orders they did, and even to filter the orders on their status: All orders, Active orders & Delivered orders. the orders are displayed in a UITableView, and I created a UISegmentedControl inside the header view to filter the orders. when the selectedSegmentIndex of that UISegmentedControl changes, it executes an NSPredicate to filter the array and display only the desired orders.
now it's working all fine, except for 1 thing: The UISegmentedControl I have does not update it's view when I select another segment. It's default selectedSegmentIndex is 'Active', because users are probably most interested in the active orders, but when I change it to 'All', the tableview displays all orders (so the predicate is working), but the view remains on the same selectedSegmentIndex.
I did a lot of research to solve this but no answer solves my problem.. my code:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if(section == 0) {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f,0.0f, 320, 45)]; // x,y,width,height
//label
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320, 20)];
title.text = #"Kies een filter...";
[headerView addSubview:title];
//segmentedcontrol
NSArray *itemArray = [NSArray arrayWithObjects: #"Alle", #"Actief", #"Afgehandeld", nil];
control = [[UISegmentedControl alloc] initWithItems:itemArray];
[control setFrame:CGRectMake(0.0f, 20.0f, 320.0, 35.0)];
control.userInteractionEnabled = YES;
control.tintColor = [UIColor blackColor];
[control setEnabled:YES];
[control addTarget:self action:#selector(changeFilter:) forControlEvents:UIControlEventAllEvents];
[headerView addSubview:control];
//label containing selected filter info
UILabel *info = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 55.0f, 320, 20)];
if ([selectedFilterInfo isEqualToString:#"Alle"]) {
info.text = #"Alle orders:";
}
else if ([selectedFilterInfo isEqualToString:#"Actief"]) {
info.text = #"Alle actieve orders:";
}
else if ([selectedFilterInfo isEqualToString:#"Afgehandeld"]) {
info.text = #"Alle afgehandelde orders:";
}
[headerView addSubview:info];
headerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"bar.png"]];
return headerView;
}
}
and the action it triggers:
- (void)changeFilter:(id)sender {
UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
if (segmentedControl.selectedSegmentIndex == 0) {
selectedFilterInfo = #"Alle";
filteredArray = nil;
[segmentedControl reloadInputViews];
[segmentedControl setSelectedSegmentIndex:0];
}
else if (segmentedControl.selectedSegmentIndex == 1) {
NSPredicate *deliveredpredicate = [NSPredicate predicateWithFormat:#"SELF.delivered contains[c] %#", #"0"];
NSMutableArray *notDeliveredArray = [NSMutableArray arrayWithArray:[sortedArray filteredArrayUsingPredicate:deliveredpredicate]];
filteredArray = [NSMutableArray arrayWithArray:notDeliveredArray];
selectedFilterInfo = #"Actief";
[segmentedControl reloadInputViews];
[segmentedControl setSelectedSegmentIndex:1];
}
else if (segmentedControl.selectedSegmentIndex == 2) {
NSPredicate *deliveredpredicate = [NSPredicate predicateWithFormat:#"SELF.delivered contains[c] %#", #"1"];
NSArray *deliveredArray = [sortedArray filteredArrayUsingPredicate:deliveredpredicate];
filteredArray = [NSMutableArray arrayWithArray:deliveredArray];
selectedFilterInfo = #"Afgehandeld";
[segmentedControl reloadInputViews];
[segmentedControl setSelectedSegmentIndex:2];
}
[self.tableView reloadData];
}
I did not include the numberOfCellsInSection etcetera because the tableview part is working perfectly. the only thing that's not working is updating the view.
Any solution would be really appreciated, Thank you all in advance!!
reloadData will reload the tableView, and when you reload the tableView all headerViews will be reloaded too. When the tableView was reloaded there is a new header. And the UISegmentedControl you see now is not the one that you have tapped.
Create an ivar that holds your selected index
#implementation .. {
NSInteger selectedIndex;
}
when creating the view restore the saved index
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
/* ... */
control = [[UISegmentedControl alloc] initWithItems:itemArray];
control.selectedSegmentIndex = selectedIndex;
/* ... */
}
save the index when changing the segmentedControl
- (void)changeFilter:(id)sender {
UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
selectedIndex = segmentedControl.selectedSegmentIndex;
if (segmentedControl.selectedSegmentIndex == 0) {
selectedFilterInfo = #"Alle";
filteredArray = nil;
}
/* ... */
// reloads the table and all header views!
[tableView reloadData];
}
And btw. when you use UISegmentedControls there is no need to call setSelectedSegmentIndex: on it again, the tap sets the index already. And reloadInputViews is totally useless for a UISegmentedControl. But I guess this code was just added because it didn't work. Don't forget to remove it ;-)
I have 2 UIpicker view in my app and I want the pickers to show the last chosen row after i saved my data. I am using this selectRow:inComponent:animated: method, but where do I exactly call this method?
in viewDidLoad:
if (self.pickerView == nil) {
self.pickerView = [[UIPickerView alloc] init];
self.pickerView.delegate = self;
self.pickerView.showsSelectionIndicator = YES;
in pickerView:viewForRow:forComponent:reusingView:
if (component == 0)
{
for (int j =20; j<=200; j++)
{
NSString* myString = [NSString stringWithFormat:#"%d",j];
[myArray addObject:myString];
}
UILabel *weightLabel0 = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, 100, 32)];
Label0.text = [myArray objectAtIndex:row];
Label0.textAlignment = UITextAlignmentLeft;
Label0.backgroundColor = [UIColor clearColor];
[rowView insertSubview:weightLabel0 atIndex:1];
return rowView;
}
and i have a method here:
-(void) refreshPicker
{
NSArray* mArray = [self.myTextField.text componentsSeparatedByString:#"."];
NSNumber* str1 = [mArray objectAtIndex:0];
int selectRow1 = [str1 intValue]-20;
NSNumber* str2 = [mArray objectAtIndex:1];
int selectRow = [str2 intValue];
if (selectRow == 0) {
int selectRow2 = 0;
[self.pickerView selectRow:selectRow2 inComponent:1 animated:NO];
}
else if (selectRow == 5) {
int selectRow2 = 1;
[self.pickerView selectRow:selectRow2 inComponent:1 animated:NO];
}
[self.pickerView selectRow:selectRow1 inComponent:0 animated:NO];
[pickerView_ reloadAllComponents];
}
how should i put this together? if i call this method at, for instance, viewDidLoad, nothing happens. help please =)
I have came up with an idea. since my pickers only create the items when the textfield is being called, i.e. becomes first responder, hence, i call my refreshPicker method inside textFieldDidBeginEditing. and don't forget to set the textfield delegate to self. =) to those who encounters my problem, cheers.
I declare my table like so:
CGRect cgRct = CGRectMake(5, 5, 310, 363);
videoTable = [[UITableView alloc] initWithFrame:cgRct style:UITableViewStylePlain];
videoTable.backgroundColor = [UIColor clearColor];
videoTable.separatorColor = [UIColor whiteColor];
videoTable.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
videoTable.dataSource = self;
videoTable.delegate = self;
videoTable.alwaysBounceVertical = NO;
videoTable.bounces = NO;
videoTable.allowsSelection = YES;
//self.videoTable.layer.zPosition = -1;
//table.dragging = NO;
[self.view addSubview:videoTable];
I remove my table when we leave the view
-(void) viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
NSLog(#"*viewDidDisappear");
if (videoTable)
{
NSLog(#"removing table");
[videoTable removeFromSuperview];
videoTable.delegate = nil;
[videoTable release];
// self.videoTable = nil;
}
}
When the view loads again there is no sign of the table, but.. it is still there because
-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self populateFileList];
if (videoTable)
{
NSLog(#"table still here");
}
else
{
NSLog(#"No table");
}
}
Can anybody explain what it is that I am missing? I want the table completely gone, so I can alloc a new table.
Many Thanks,
-Code
You need to assign nil to the table after you release it, otherwise, the variable still have a value (an old reference to the table object):
[videoTable release];
videoTable = nil;
You can try this:
-(void)viewDidUnload{
[super viewDidUnload];
[self setVideoTable:nil];
}
if (videoTable) is true if videoTable is different from nil. So you need to assign your table to nil after releasing it.
I make one tableview in which I add button type cell using custom cell. Looks good but when there is less cell in tableview at that time separator looks very bad. In design so there is any solution than tell me.
I try to remove separator from design but it does not work.
- (void)viewDidLoad {
self.navigationItem.title = #"My List";
self.tblView.separatorStyle = UITableViewCellSeparatorStyleNone;
[[self tableView] setRowHeight:70];
[[self tableView] setBackgroundColor:[UIColor blackColor]];
Helath_ndroidAppDelegate *appDeleg = (Helath_ndroidAppDelegate *)[[UIApplication sharedApplication] delegate];
NSMutableArray *temp1 =[[NSMutableArray alloc] init];
appDeleg.recipCategoryFromDatabase = temp1;
[temp1 release];
dataBase *objData = [[[dataBase alloc] init] autorelease];
[objData selectrecepFromDatabase];
[[self tableView] setSeparatorStyle:UITableViewCellSeparatorStyleSingleLineEtched];
[super viewDidLoad];
}
Have you tried to remove separator programmatically?
I use this code with my custom cells...
In your viewDidLoad method add this line of code if you are using an UITableViewController:self.yourTableViewHere.separatorStyle = UITableViewCellSeparatorStyleNone;
How can I set a TTStyledTextLabel inside of a UITableView.
Each TTStyledTextLabel contains Some parsed HTML.
Heres what I have I realize its probably completely wrong.
TTStyledTextLabel* label = [[TTStyledTextLabel alloc] autorelease];
cell.textLabel.text = [TTStyledText textFromXHTML:tempString lineBreaks:YES URLs:YES];
App Crashes on launch. I think its because I am setting the .text property with something that is not text. However, I don't know what else to set.
The following code will do what you want. Unfortunately, however, I cannot figure out how to automatically set the height. If memory isn't an issue you could keep a seperate array of TTStyledTextLabels and reference their heights.
in your loadView:
CGRect cgRct2 = CGRectMake(0, 35, 320, 375); //define size and position of view
tblView = [[UITableView alloc] initWithFrame:cgRct2 style:UITableViewStylePlain];
tblView.dataSource = [self constructDataSource];
tblView.delegate = self;
//[tblView reloadData];
[myView addSubview:tblView];
in your class:
-(TTListDataSource *)constructDataSource {
NSLog(#"constructDataSource");
NSMutableArray * namesArray = [[NSMutableArray alloc] init];
//ADD ITEMS
[namesArray addObject:[TTStyledText textFromXHTML:[NSString stringWithString:#"some XHTML"]]];
TTListDataSource * dataSource = [[TTListDataSource alloc] init];
for (int i = 0; i < [namesArray count]; i++) {
TTStyledText * text = [namesArray objectAtIndex:i];
[dataSource.items addObject:[TTTableStyledTextItem itemWithText:text]];
}
[namesArray release];
return dataSource;
}