Using Autolayout to position UITextField next to UILabel - ios6

I would like to use auto layout to position a UITextField next to cell.textLabel when a UITableView goes into edit mode. The code that I have works correctly but I get a message in the log that says some existing constraints had to be broken.
UILabel *label = cell.textLabel;
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0.0, 0.0, 400.0, 22.0)];
textField.placeholder = cell.textLabel.text;
textField.translatesAutoresizingMaskIntoConstraints = NO;
textField.text = text;
[cell.contentView addSubview:textField];
[cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:[label]-[textField]-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(label,textField)]];
[cell addConstraint:[NSLayoutConstraint constraintWithItem:textField attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:label attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
Log message is:
(
"<NSAutoresizingMaskLayoutConstraint:0x1066abc0 h=--& v=--& H:[UITableViewCellContentView:0x9a7d070(934)]>",
"<NSAutoresizingMaskLayoutConstraint:0x10660a90 h=--& v=--& H:[UILabel:0x9a93ef0(914)]>",
"<NSLayoutConstraint:0x1065ea60 H:[UITextField:0x1065c660]-(NSSpace(20))-| (Names: '|':UITableViewCellContentView:0x9a7d070 )>",
"<NSAutoresizingMaskLayoutConstraint:0x10660a50 h=--& v=--& UILabel:0x9a93ef0.midX == + 467>",
"<NSLayoutConstraint:0x10669280 H:[UILabel:0x9a93ef0]-(NSSpace(8))-[UITextField:0x1065c660]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x10669280 H:[UILabel:0x9a93ef0]-(NSSpace(8))-[UITextField:0x1065c660]>
I think that this is caused by conflicting restraints with the cell.textLabel width. So my question is if there is another way to have a textfield in a standard table view cell that stretches from the textLabel width to the end of the cell without breaking default constraints. I feel like I'm close but I can't quite get there. I've searched Google over for three weeks but can't quite get my head around this. I've also watched WWDC videos on auto layout (perhaps I'm just an idiot). Thanks for your help.

Instead of trying to manipulate Apple's standard cell, I took the plunge and wrote my own UITableViewCell subclass that mimics UITableViewCellStyleValue1. When the tableview goes into edit mode, in the simplest terms I hide the value label and display the textfield. For those who might be struggling with the same thing, I'm posting some code to help you get started:
#interface NXAlphaNumericTextFieldCell : UITableViewCell<UITextFieldDelegate,NumberKeyboardDelegate>
#property (strong, nonatomic) UITextField *inputTextField;
#property (strong, nonatomic) UILabel *titleLabel;
#property (strong, nonatomic) UILabel *valueLabel;
#property (strong, nonatomic) NSArray *xTitleLabelConstraints;
#property (strong, nonatomic) NSArray *xTextFieldConstraints;
#property (strong, nonatomic) NSArray *xValueLabelConstraints;
#end
And a few methods in the implementation:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 44.0f)];
self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
self.titleLabel.font = [UIFont boldSystemFontOfSize:16.0f];
self.titleLabel.backgroundColor = [UIColor clearColor];
self.valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 44.0f)];
self.valueLabel.translatesAutoresizingMaskIntoConstraints = NO;
self.valueLabel.textColor = [UIColor colorWithRed:81.0/255.0 green:102.0/255.0 blue:145.0/255.0 alpha:1.0];
self.valueLabel.backgroundColor = [UIColor clearColor];
self.inputTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 44.0f)];
self.inputTextField.translatesAutoresizingMaskIntoConstraints = NO;
self.inputTextField.autocapitalizationType = UITextAutocapitalizationTypeWords;
self.inputTextField.autocorrectionType = UITextAutocorrectionTypeYes;
self.inputTextField.clearButtonMode = UITextFieldViewModeAlways;
self.inputTextField.delegate = self;
[self.contentView addSubview:self.valueLabel];
[self.contentView addSubview:self.titleLabel];
[self.contentView addSubview:self.inputTextField];
UILabel *textLabel = self.titleLabel;
NSDictionary *labelTextFieldViewsDictionary = NSDictionaryOfVariableBindings(textLabel);
self.xTitleLabelConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"|-[textLabel]"
options:0
metrics:nil
views:labelTextFieldViewsDictionary];
UITextField *textfield = self.inputTextField;
labelTextFieldViewsDictionary = NSDictionaryOfVariableBindings(textLabel, textfield);
self.xTextFieldConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"|-[textLabel]-50-[textfield]-|"
options:0
metrics:nil
views:labelTextFieldViewsDictionary];
UILabel *valueLabel = self.valueLabel;
labelTextFieldViewsDictionary = NSDictionaryOfVariableBindings(valueLabel);
self.xValueLabelConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"H:[valueLabel]-|"
options:0
metrics:nil
views:labelTextFieldViewsDictionary];
[self setNeedsUpdateConstraints];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
if (self.isEditing) {
[self.inputTextField becomeFirstResponder];
}
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[self addConstraints:self.xTitleLabelConstraints];
if (editing) {
if (self.inputType == kCellInputTypeAlphaNumeric) {
self.inputTextField.keyboardType = UIKeyboardTypeAlphabet;
} else if (self.inputType == kCellInputTypeEmail) {
self.inputTextField.keyboardType = UIKeyboardTypeEmailAddress;
} else if (self.inputType == kCellInputTypePhoneNumber) {
self.inputTextField.keyboardType = UIKeyboardTypeNamePhonePad;
} else {
if (!self.numberKeyboard) {
self.numberKeyboard = [[NumberKeyboard alloc] initWithNibName:#"NumberKeyboard" bundle:nil];
self.numberKeyboard.textField = self.inputTextField;
self.numberKeyboard.showsPeriod = YES;
self.numberKeyboard.delegate = self;
}
self.inputTextField.inputView = self.numberKeyboard.view;
}
self.inputTextField.text = self.valueLabel.text;
self.inputTextField.placeholder = self.titleLabel.text;
self.valueLabel.hidden = YES;
self.inputTextField.hidden = NO;
[self removeConstraints:self.xValueLabelConstraints];
[self addConstraints:self.xTextFieldConstraints];
} else {
[self.inputTextField resignFirstResponder];
self.inputTextField.hidden = YES;
self.valueLabel.hidden = NO;
[self removeConstraints:self.xTextFieldConstraints];
[self addConstraints:self.xValueLabelConstraints];
}
}
- (void)updateConstraints
{
[super updateConstraints];
if (self.editing) {
[self removeConstraints:self.xValueLabelConstraints];
[self addConstraints:self.xTextFieldConstraints];
} else {
[self removeConstraints:self.xTextFieldConstraints];
[self addConstraints:self.xValueLabelConstraints];
}
}

Related

SceneKit: how to create chessboard pattern for SCNFloor from image?

The goal is to create an infinite chessboard pattern.
Using a SCNFloor and the attached image, we produce something close but not quite like a chessboard. Some black squares merge where they shouldn't.
We tried different values for Scale, WrapS, WrapT, Min filter, Map filter, and Mip filter. The screenshot shows the current values.
Is the underlying image not correct, or what setting do we need to change for the SCNFloor?
Repeated image:
Result:
#import "GameViewController.h"
#interface GameViewController ()
#property (nonatomic) CGFloat chessBoardWidth;
#property (nonatomic) CGFloat chessBoardDepth;
#property (nonatomic) CGFloat tileWidth;
#property (nonatomic) CGFloat tileDepth;
#property (nonatomic, getter=isOdd) BOOL odd;
#end
#implementation GameViewController
-(instancetype)init {
self = [super init];
if(self) {
self.chessBoardWidth = 10.0f;
self.chessBoardDepth = 10.0f;
self.tileWidth = 1.0f;
self.tileDepth = 1.0f;
}
return self;
}
-(void)awakeFromNib
{
[super awakeFromNib];
// create a new scene
SCNScene *scene = [SCNScene sceneNamed:#"art.scnassets/chessboard.scn"];
// create and add a camera to the scene
SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
[scene.rootNode addChildNode:cameraNode];
// place the camera
cameraNode.position = SCNVector3Make(0, 0, 150);
// create and add a light to the scene
SCNNode *lightNode = [SCNNode node];
lightNode.light = [SCNLight light];
lightNode.light.type = SCNLightTypeOmni;
lightNode.position = SCNVector3Make(0, 10, 10);
[scene.rootNode addChildNode:lightNode];
// create and add an ambient light to the scene
SCNNode *ambientLightNode = [SCNNode node];
ambientLightNode.light = [SCNLight light];
ambientLightNode.light.type = SCNLightTypeAmbient;
ambientLightNode.light.color = [NSColor darkGrayColor];
[scene.rootNode addChildNode:ambientLightNode];
// Material
SCNMaterial *blackMaterial = [SCNMaterial material];
blackMaterial.diffuse.contents = [NSColor blackColor];
SCNMaterial *whiteMaterial = [SCNMaterial material];
whiteMaterial.diffuse.contents = [NSColor whiteColor];
// Geometry
SCNPlane *blackTile = [[SCNPlane alloc] init];
blackTile.firstMaterial = blackMaterial;
SCNPlane *whiteTile = [[SCNPlane alloc] init];
whiteTile.firstMaterial = whiteMaterial;
// Parent node
SCNNode *parentNode = [[SCNNode alloc] init];
[scene.rootNode addChildNode:parentNode];
self.odd = YES;
for (uint x=0; x < self.chessBoardWidth; x++) {
for (uint z=0; z < self.chessBoardDepth; z++) {
// Add tile
SCNNode *tileNode = [[SCNNode alloc] init];
if(self.isOdd) {
tileNode.geometry = blackTile;
} else {
tileNode.geometry = whiteTile;
}
[parentNode addChildNode:tileNode];
// Position tile
tileNode.position = SCNVector3Make(self.tileWidth * x, 0, self.tileDepth * z);
// Alternate
if(self.isOdd) {
self.odd = NO;
} else {
self.odd = YES;
}
}
}
// set the scene to the view
self.gameView.scene = scene;
// allows the user to manipulate the camera
self.gameView.allowsCameraControl = YES;
// show statistics such as fps and timing information
self.gameView.showsStatistics = YES;
// configure the view
self.gameView.backgroundColor = [NSColor grayColor];
}
#end

MPMoviePlayerController load video from Photo library,play so fast

// CameraViewController.h
#interface CameraViewController : UIViewController{
UIImagePickerController *_picker;
}
#property (strong, nonatomic) IBOutlet UIImageView *imageView;
#property (nonatomic, retain) MPMoviePlayerController *player;
(IBAction)loadImage:(UIButton *)sender;
// CameraViewController.m
#interface CameraViewController ()
#property (strong, nonatomic) MPMoviePlayerController *playView;
#end
#implementation CameraViewController
#synthesize imageView = _imageView;
#synthesize player = _player;
#synthesize playView = _playView;
(void)viewDidLoad
{
[super viewDidLoad];
_picker = [[UIImagePickerController alloc] init];
}
(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
(IBAction)loadImage:(UIButton *)sender {
_picker.delegate = self;
_picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
NSArray *mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, (NSString *)kUTTypeMovie, nil];
_picker.mediaTypes = mediaTypes;
[self presentViewController:_picker animated:YES completion:NULL];
}
pragma mark -ImagePicker Delegate
(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = nil;
NSURL *imageURL = nil;
imageURL = [info valueForKey:UIImagePickerControllerMediaURL];
if (imageURL == nil) {
image = [info valueForKey:UIImagePickerControllerEditedImage];
if (image == nil) {
image = [info valueForKey:UIImagePickerControllerOriginalImage];
_imageView.image = image;
}
else {
}
}
else {
self.player = [[MPMoviePlayerController alloc] initWithContentURL:imageURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.player];
self.player.view.frame = CGRectMake(10.0, 10.0, 200.0, 200.0);
self.player.view.backgroundColor = [UIColor clearColor];
self.player.movieSourceType = MPMovieSourceTypeFile;
[self.player prepareToPlay];
[self.view addSubview:self.player.view];
[self.player play];
}
[picker dismissViewControllerAnimated:YES completion:NULL];
}
(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
pragma mark - User Define
(void)movieFinishedCallback:(NSNotification *)notification {
MPMoviePlayerViewController *player = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification object:player];
[player.view removeFromSuperview];
}
The movieFinishedCallback:method will be called immediately,so the video can not play completely.
How to resolve it?
Apple suggests:
Uses AVPlayer to play videos from the iPod Library, Camera Roll, or via iTunes File Sharing.

How to load URL on UITableViewCell touch

New at this, just trying to make a link load if a certain UITableView cell is touched.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
NSArray *deals = [dic objectForKey:#"deals"];
NSDictionary *dealOne = [deals objectAtIndex:0];
NSString *url = [dealOne objectForKey:#"url"];
UITouch *touch = [[event allTouches] anyObject];
if([touch isKindOfClass:[UITableViewCell class]])
{
UITableViewCell *cell;
CGPoint location = [touch locationInView: self.view];
cell.center = location;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
NSLog(#"TABLECELL TOUCHED");
}
Solved using the following. In this case, I wanted to have a link load when the cell was touched for "finalDealOne." When the cell is double-tapped, the link loads.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
NSArray *deals = [dic objectForKey:#"deals"];
NSDictionary *dealOne = [deals objectAtIndex:0];
NSString *title = [dealOne objectForKey:#"title"];
NSString *finalDealOne = [NSString stringWithFormat:#"Deal One:\n\n''%#''\n\nDouble tap here to redeem offer.",title];
(NSIndexPath *)indexPath
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(finalDealOne)];
tapped.numberOfTapsRequired = 2;
[cell addGestureRecognizer:tapped];
}
To make this work, I included the following reference:
- (void)finalDealOne
{
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
NSArray *deals = [dic objectForKey:#"deals"];
NSDictionary *dealOne = [deals objectAtIndex:0];
NSString *url = [dealOne objectForKey:#"url"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}

Is there a way select a specific image for use in a UITable iOS?

I have a UITable, and I like to add an image in the detail view for a cell. I can handle the selection for any image from the camera roll or from the camera:
cell.imageView.image = someImage;
But how do I define a specific image - in the case above: "someImage", so that the next time the app is run, the correct image is shown for each item.
UPDATE. This is the code I'm using to snap/select an image..
- (IBAction)btnTakePicture_Clicked:(id)sender
{
NSLog(#"%s", __FUNCTION__);
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Select Image from..." delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Camera", #"Image Gallary", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
actionSheet.alpha=0.90;
actionSheet.tag = 1;
[actionSheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(#"%s", __FUNCTION__);
switch (actionSheet.tag)
{
case 1:
switch (buttonIndex)
{
case 0:
{
#if TARGET_IPHONE_SIMULATOR
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Ooops" message:#"Camera not available." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
#elif TARGET_OS_IPHONE
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
//picker.allowsEditing = YES;
[self presentViewController:picker animated:YES completion:nil];
#endif
}
break;
case 1:
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
break;
}
break;
default:
break;
}
}
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
NSLog(#"%s", __FUNCTION__);
dataImage = UIImageJPEGRepresentation([info objectForKey:#"UIImagePickerControllerOriginalImage"],1);
imgPicture.image = [[UIImage alloc] initWithData:dataImage];
[picker dismissViewControllerAnimated:YES completion:nil];
}
UPDATE 2. With the help from people below, I think this solution will work for me:
- (IBAction)photoLibraryAction:(id)sender
{
int c = self.capturedImages.count;
for (int i=0; i < c; i++ ){
if (self.imageView.tag == cellTag) {
NSLog(#"found it");
} else {
NSLog(#"can't find it");
}
}
}
if ([self.capturedImages count] == 1)
{
// we took a single shot
[self.imageView setImage:[self.capturedImages objectAtIndex:0]];
[self.imageView setTag:myTag];
}
I would do in this way.
#interface CustomClassCell : UITableViewCell
#property (unsafe_unretained, nonatomic) IBOutlet UIImageView *HomePicture;
#end
#interface CustomClass : NSObject
#property (nonatomic, copy) NSString *PicPath;
#end
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomClassCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CustomClassCellIdentifer"];
id object = [self.tableData objectAtIndex:indexPath.row];
CustomClass *myObject = (CustomClass*)object;
cell.HomePicture.image = [UIImage imageNamed:myObject.PicPath];
return cell;
}

Custom swipe function in UITableViewCell doesn't work

I need to add the count in the uitableviewcell in a such a way that when I trigger the swipe function the count should be incremented in the corresponding cell and while tapping the count should be decremented.
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
NSString *CellIdentifier = #"sample";
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
UISwipeGestureRecognizer *recognizer;
recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapFrom:)];
[self addGestureRecognizer:recognizer];
self.tapRecognizer = (UITapGestureRecognizer *)recognizer;
recognizer.delegate = self;
[recognizer release];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
[self addGestureRecognizer:recognizer];
[recognizer release];
UILabel *cookieLabel = [[UILabel alloc] initWithFrame:CGRectMake(5,5, 120,30)];
cookieLabel.text = #"hello";
cookieLabel.font = [UIFont systemFontOfSize:15.0f];
cookieLabel.textColor = [UIColor blackColor];
cookieLabel.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:cookieLabel];
[cookieLabel release];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
costLabel = [[UILabel alloc] initWithFrame:CGRectMake( 200, 5, 230, 30)];
//costLabel.text = handleSwipeFrom:;
costLabel.font = [UIFont systemFontOfSize:15.0f];
costLabel.textColor = [UIColor blackColor];
costLabel.backgroundColor = [UIColor greenColor];
[cell.contentView addSubview:costLabel];
[costLabel release];
[self setUserInteractionEnabled:YES];
return cell;
}
Don't add the UISwipeGestureRecognizer to the cell. Add it to the UITableView.
I used TISwipeableTableView as a base and modified it heavily to work correctly (they did their own touch handling, which resulted in a "weird, unnative" feeling)
- (void)didSwipe:(UIGestureRecognizer *)gestureRecognizer {
if ([MRUserDefaults sharedMRUserDefaults].isSwipeMenuEnabled) {
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
CGPoint swipeLocation = [gestureRecognizer locationInView:self];
NSIndexPath *swipedIndexPath = [self indexPathForRowAtPoint:swipeLocation];
TISwipeableTableViewCell* swipedCell = (TISwipeableTableViewCell *)[self cellForRowAtIndexPath:swipedIndexPath];
if ([swipedCell isKindOfClass:[TISwipeableTableViewCell class]]) {
if (![swipedIndexPath isEqual:indexOfVisibleBackView]) {
[self hideVisibleBackView:YES];
[swipedCell revealBackView];
[self setIndexOfVisibleBackView:swipedIndexPath];
if (swipeDelegate && [swipeDelegate respondsToSelector:#selector(tableView:didSwipeCellAtIndexPath:)]){
[swipeDelegate tableView:self didSwipeCellAtIndexPath:[self indexPathForRowAtPoint:swipeLocation]];
}
}
}
}
}
}
- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
if ((self = [super initWithFrame:frame style:style])) {
if ([MRUserDefaults sharedMRUserDefaults].isSwipeMenuEnabled) {
UIGestureRecognizer *swipeGesture = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(didSwipe:)] autorelease];
[self addGestureRecognizer:swipeGesture];
}
}
return self;
}
This should get you started.
[cell addGestureRecognizer:recognizer]

Resources