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 ;-)
Related
My code parts in two:
The first is the "cellForRowAtIndexPath" method for my table view. In this table view i'm supposed to show 3 buttons in each cell and each button is a different image. Images are in an NSMutableArray call "photosArray". It looks like 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 (photosArray.count > 0) {
for (int i = 0; i < 3; i++) {
if (photosArray.count > photoCounter) {
//position the UiButtons in the scrollView
CGRect frame = CGRectMake((i*100) + 30, 5, 60, 60);
//Create the UIbuttons
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTag:i];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.frame = frame;
button.layer.borderWidth = 2;
button.layer.cornerRadius = 5;
button.clipsToBounds = YES;
//Add images to the button
NSLog(#"PhotosArray count is: %d",photosArray.count);
if ([[photosArray objectAtIndex:0] isKindOfClass:[UIImage class]]) {
NSLog(#"UIImage class true");
}
if ([[photosArray objectAtIndex:0] isMemberOfClass:[UIImage class]]) {
NSLog(#"UIImage class true");
}
UIImage *btnImg = [self.photosArray objectAtIndex:photoCounter];
//UIImage *btnImg2 = [UIImage imageNamed:#"GameOver"];
photoCounter++;
[button setBackgroundImage:btnImg forState:UIControlStateNormal];
[cell.contentView addSubview:button];
}
}
}else{
UILabel *noImages = [[UILabel alloc] initWithFrame:CGRectMake(0, 25, 320, 20)];
noImages.text = #"No images";
noImages.textAlignment = NSTextAlignmentCenter;
[cell.contentView addSubview:noImages];
}
return cell;
}
The second part is where I load my pictures into "photosArray". I am using WSAssetPicker to load multiple photos from my asset library.
Here is the code:
- (void)assetPickerController:(WSAssetPickerController *)sender didFinishPickingMediaWithAssets:(NSArray *)assets
{
// Dismiss the WSAssetPickerController.
[self dismissViewControllerAnimated:YES completion:^{
arep = [[ALAssetRepresentation alloc] init];
for (ALAsset *asset in assets) {
UIImage *imageA = [[UIImage alloc] initWithCGImage:asset.defaultRepresentation.fullScreenImage];
[self.photosArray addObject:imageA];
}
NSLog(#"%d",photosArray.count);
photoCounter = 0;
[self.tableView reloadData];
}];
}
And now for the issue, the buttons that are created have no image in it. I only get a transparent button with a border and rounded corners.
Why is that?
It was recently brought to my attention that I omitted an important piece from the README (which is now updated).
It is necessary to hold a strong reference of WSAssetPickerController if you are accessing ALAsset objects in the dismissViewControllerAnimated:completion: completion block.
If you do not hold a strong reference to the picker controller before calling dismissViewControllerAnimated:completion: the picker controller will be released before the completion block is executed resulting in the ALAssetsLibrary (used internally in the WSAssetPicker code) from being released before you try to access the ALAssets in the assets array passed in the assetPickerController:didFinishPickingMediaWithAssets: delegate method.
If ALAssetsLibrary gets released, the ALAssets owned by the ALAssetsLibrary will expire and calls to asset.defaultRepresentation.fullScreenImage will return null.
The following will preserve the ALAssetsLibrary while dismissing the picker controller's view:
- (void)assetPickerController:(WSAssetPickerController *)sender didFinishPickingMediaWithAssets:(NSArray *)assets
{
// Hang on to the picker to avoid ALAssetsLibrary from being released.
self.picker = sender;
__block id weakSelf = self;
// Note: Instead of `id` you could specify the class of `self` order to access properties with dot notation.
// Dismiss the WSAssetPickerController.
[self dismissViewControllerAnimated:YES completion:^{
// Do something with the assets here.
// Release the picker.
[weakSelf setPicker:nil];
}];
}
An alternative would be to process the array of ALAssets before calling dismissViewControllerAnimated:completion:, but that could cause the picker controller's dismissal to delay resulting in a poor user experience.
I have created 4 buttons via Interface Builder. I have an array which has the following content stored in it
"A"
"B"
"B"
"A"
Now what i need to do is to read the array of strings and paint the button Yellow or Red. If the String is A then the button should be Yellow, and if the String character is B the button should be Red.
My code so far;
for (NSString* content in arr) {
if ([content isEqualToString:#"A"]){
// Make the (1st/there after) button in the interface builder to Yellow and etc
}else {
// Make the 1st button in the interface builder to Redand etc
}
}
First, in Interface Builder (IB) you need to assign tag's to each button that you want to change so that you can retrieve them later. Set the first button tag to 0, second to 1, etc.
Then your code would look something like this:
for (NSUInteger i = 0; i < 4; i++) {
UIButton *button = (UIButton *)[self.view viewWithTag:i];
if ([(NSString *)[arr objectAtIndex:i] isEqualToString:#"A"]) {
[button setBackgroundColor:[UIColor yellowColor]];
} else {
[button setBackgroundColor:[UIColor redColor]];
}
}
You should tag the buttons in IB, or create Outlets for them in the subclass. Then you can reference them by these tags or outlets.
Once you have created all of the outlets, you could store the references in a member variable containing the outlets. Then it is a simple matter of setting the background color to the correct value based on the string.
Actually, you can use tag property of UIButton. Suppose, your button tags will be 1, 2, 3, 4. So:
NSArray *array = [NSArray arrayWithObjects:#"A", #"B", #"B", #"A", nil];
for (int i = 0; i < 4; i++) {
UIView *view = [self.view viewWithTag:i + 1];
if ([[array objectAtIndex:i] isEqualToString:#"A"])
view.backgroundColor = [UIColor yellowColor];
else if ([[array objectAtIndex:i] isEqualToString:#"B"])
view.backgroundColor = [UIColor redColor];
}
Besides, I wouldn't recommend you to use IB, especially at the beginning. Write the code by yourself.
This code may contain syntax or logic errors (I wrote it in notepad):
#interface ViewController : UIViewController
#end
//
#implementation ViewController
- (void)loadView {
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view.backgroundColor = [UIColor whiteColor];
NSArray *array = [NSArray arrayWithObjects:#"A", #"B", #"B", #"A", nil];
CGFloat offset = 5.0f, step = self.view.frame.size.width / [array count];
for (NSString *string in array) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(offset, 5, step - 10, 50);
offset += step;
if ([string isEqualToString:#"A"])
button.backgroundColor = [UIColor yellowColor];
else if ([string isEqualToString:#"B"])
button.backgroundColor = [UIColor redColor];
[self.view addSubview:button];
}
}
#end
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.
i want to display the table view in alert view. This table view contains segmented control.
these segmented controls are for on/off the audio, image, text. so there will be 3 cells with segmented controls.
how to do this
please help me out
Thank u
Adding UISegmentedControls (or UISwitches, which I would personally recommend for a simple on/off option) is easy enough to put in table view cells using the accessoryView property.
Your view controller (or whatever object you're using to control this thing) must implement the UITableViewDataSource protocol.
#interface MyViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> {}
#end
Add the controls in tableView:cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString* const SwitchCellID = #"SwitchCell";
UITableViewCell* aCell = [tableView dequeueReusableCellWithIdentifier:SwitchCellID];
if( aCell == nil ) {
aCell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SwitchCellID] autorelease];
aCell.textLabel.text = [NSString stringWithFormat:#"Option %d", [indexPath row] + 1];
aCell.selectionStyle = UITableViewCellSelectionStyleNone;
UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
aCell.accessoryView = switchView;
[switchView setOn:YES animated:NO];
[switchView addTarget:self action:#selector(soundSwitched:) forControlEvents:UIControlEventValueChanged];
[switchView release];
}
return aCell;
}
Or, if you're set on segmented controls, replace the UISwitch stuff above with something like:
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"On", #"Off", nil]];
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.frame = CGRectMake(75, 5, 130, 30);
segmentedControl.selectedSegmentIndex = 0;
[segmentedControl addTarget:self action:#selector(controlSwitched:) forControlEvents:UIControlEventValueChanged];
aCell.accessoryView = segmentedControl;
[segmentedControl release];
I would personally put these options in a regular view (probably accessible from an options button which does the flip transition, like many iPhone apps do). I don't think putting it in an alert view is a very good user experience.
However, I have written a few blog posts showing how to put various views into alert views (text field, which is kinda useful, and web view, which is arguably not).
So, if you're really dead set on putting this in an alert view, you can totally do that, like this:
- (void) doAlertWithListView {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Preferences"
message:#"\n\n\n\n\n\n\n"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
UITableView *myView = [[[UITableView alloc] initWithFrame:CGRectMake(10, 40, 264, 150)
style:UITableViewStyleGrouped] autorelease];
myView.delegate = self;
myView.dataSource = self;
myView.backgroundColor = [UIColor clearColor];
[alert addSubview:myView];
[alert show];
[alert release];
}
It will look something like this:
The answer above works great, but on the iPad the line myView.backgroundColor = [UIColor clearColor];
doesn't seem to remove the grey rectangular area around the table. I find that the following does the job: tableView.backgroundView.alpha = 0.0;
I created a custom UITableViewCell class with a UIButton, a UIImage, and two UILabels. The button and the image are overlayed on top of each other, and only one is displayed at a time. The expected behavior is you touch on the button, the button disappears, and it displays the image. The UITableViewCells are set to be reused. Here's my code:
Constructor:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
unheartButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
unheartButton.backgroundColor = [UIColor clearColor];
unheartButton.frame = CGRectMake(10, 13, 20, 18);
[unheartButton addTarget:self action:#selector(onButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[unheartButton setBackgroundImage:[UIImage imageNamed:#"redheart.png"] forState:UIControlStateNormal];
imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(12, 13, 16, 16);
NSMutableArray *array = [[NSMutableArray alloc] init];
for (int ndx = 1; ndx < 13; ndx++) {
[array addObject:[UIImage imageNamed:[NSString stringWithFormat:#"icon-loading-%d (dragged).tiff", ndx]]];
}
imageView.animationImages = array;
imageView.animationDuration = 1;
[array release];
[self.contentView addSubview:imageView];
[self.contentView addSubview:unheartButton];
return self;
}
}
- (void) setModel:(MyModel *)myModel {
model = myModel;
if (model.hideImage) {
imageView.hidden = YES;
unheartButton.hidden = NO;
else {
imageView.hidden = NO;
unheartButton.hidden = YES;
}
}
Button click:
- (IBAction) onButtonClick: (id) sender {
model.hideImage = NO;
unheartButton.hidden = YES;
imageView.hidden = NO;
[imageView startAnimating];
[self setNeedsDisplay];
[self.contentView setNeedsDisplay];
[self.unheartButton setNeedsDisplay];
[self.imageView setNeedsDisplay];
}
I'm calling setNeedsDisplay on everything, but nothing seems to happen. If I scroll off the screen and back up, the button is hidden and now the loading icon is shown, but this only happens after a scroll. I'm not sure what I need to do to get the cell to repaint.
The UITableView does some fancy caching to support scrolling and the like; I've had similar issues with refreshing a specific cell when I use custom views.
You can use reloadRowsAtIndexPaths:withRowAnimation: to make the table reload just that row. See this post for more information: Why won't my UITableViewCell deselect and update its text?