How Can I Set the Height of a Single UITabelViewCell to a Constant Value? - objective-c

I am searching on Google about this but I always find very complex solutions about it. I have a cell with an non editable scrollable UITextView and I just want to increase the height of that cell to a constant value. How can I do it? Actually I am using the next code to add the UITextView to the cell.
cell.contentView.bounds = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height);
UITextView *textView = [[UITextView alloc] initWithFrame:cell.contentView.bounds];
textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
textView.editable = NO;
textView.scrollEnabled = YES;
textView.backgroundColor = [UIColor clearColor];
textView.text = self.description;
[cell.contentView addSubview:textView];
[textView release];
Thanks for reading.

Implement this method in your delegate: http://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:heightForRowAtIndexPath:

Related

UItextfield adding extra spaces beneath timestamp

I have a relatively simple comment box (UITextfield) for a photo app into which I've found that when entering long comments (usually more than half a dozen words) a lot of blank space appears below the timestamp ("X minutes ago") within the comment box. The longer the post, the more blank space appears within the box. Everything else works fine, the comment is vertically aligned correctly, and the timestamp is directly below this.
self.backgroundColor = [UIColor clearColor];
mainView = [[UIView alloc] initWithFrame:CGRectMake( 20.0f, 0.0f, 280.0f, 51.0f)];
mainView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"BackgroundComments.png"]];
[self addSubview:mainView];
UIImageView *messageIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"IconAddComment.png"]];
messageIcon.frame = CGRectMake( 9.0f, 17.0f, 19.0f, 17.0f);
[mainView addSubview:messageIcon];
UIImageView *commentBox = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:#"TextFieldComment.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5.0f, 10.0f, 5.0f, 10.0f)]];
commentBox.frame = CGRectMake(35.0f, 8.0f, 237.0f, 35.0f);
[mainView addSubview:commentBox];
commentField = [[UITextField alloc] initWithFrame:CGRectMake( 40.0f, 10.0f, 227.0f, 31.0f)];
commentField.font = [UIFont systemFontOfSize:14.0f];
commentField.placeholder = #"Add a comment";
commentField.returnKeyType = UIReturnKeySend;
commentField.textColor = [UIColor colorWithRed:73.0f/255.0f green:55.0f/255.0f blue:35.0f/255.0f alpha:1.0f];
commentField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[commentField setValue:[UIColor colorWithRed:154.0f/255.0f green:146.0f/255.0f blue:138.0f/255.0f alpha:1.0f] forKeyPath:#"_placeholderLabel.textColor"];
[mainView addSubview:commentField];
Any help would be much appreciated!

Horizontal UIScrollView in a UITableViewCell

I'm trying to create a simple scrollView inside a tableview cell. Just like on app store, but with simpler scrolling.
The scrollview is showing up but its not scrolling at all. I've tried whole first page on google and many stackoverflow questions, can't seem to find what I'm doing wrong.
if (indexPath.row==0) {
// Clearing out cell background
[cell setBackgroundColor:[UIColor clearColor]];
[cell.contentView setBackgroundColor:[UIColor clearColor]];
// Imageviews for scrollview
UIImageView *imageview1 = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,123)];
UIImageView *imageview2 = [[UIImageView alloc] initWithFrame:CGRectMake(320,0,320,123)];
imageview1.image = [UIImage imageNamed:#"flash2.png"];
imageview2.image = [UIImage imageNamed:#"IMG_2458.JPG"];
// setting up scrollview
self.scrollViewHeader = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,320, 123)];
self.scrollViewHeader.scrollEnabled = YES;
self.scrollViewHeader.delegate = self;
[self.scrollViewHeader setUserInteractionEnabled:YES];
[self.scrollViewHeader setContentSize:CGSizeMake(320, 123)];
[self.scrollViewHeader addSubview:imageview1];
[self.scrollViewHeader addSubview:imageview2];
[cell.contentView addSubview:self.scrollViewHeader];
}
else{
// Normal table cells.
...
}
Thanks in advance for your answers.
In order for a scroll view to scroll, its content size must be greater than its bounds.
You have:
self.scrollViewHeader = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,320, 123)];
and then
[self.scrollViewHeader setContentSize:CGSizeMake(320, 123)];
In this case, they are both the same size, therefore it won't scroll. Do:
self.scrollViewHeader = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,160, 123)];
[self.scrollViewHeader setContentSize:CGSizeMake(320, 123)];

setting alpha of UITextField inputAccessoryView does not work

The inputAccessoryView is shown successfully, but it is totally black.
_textField = [[UITextField alloc] initWithFrame: CGRectZero]; // update frame later
_textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
UIView *inputAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)];
inputAccessoryView.backgroundColor = [UIColor blackColor];
inputAccessoryView.alpha = 0.2;
_textField.inputAccessoryView = inputAccessoryView;
I am completely confused over this... can anyone explain why it happens?
Use This method of UIColor on previous line instead of setting alpha:-
[UIColor colorWithHue:saturation:brightness:alpha:];

Changing elements in selected UITableViewCell subclass

I have implemented a custom UITableViewCell for my application and have created a custom background view and selectedbackgroundview, both of which work great. However, I have several other images and uilabels on the cell that I want to change the color's of when it is selected. I have overwritten the following method and it logs correctly when the cell is selected but it does not change the image or the text color.
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
if (selected) {
NSLog(#"setSelected:YES");
separatorImage.image = [UIImage imageNamed:#"SeparatorSelected"];
titleLabel.textColor = [UIColor whiteColor];
} else {
NSLog(#"setSelected:NO");
separatorImage.image = [UIImage imageNamed:#"Separator"];
titleLabel.textColor = [UIColor grayColor];
}
}
Any Idea what I am doing wrong?
Update
separatorImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Separator"]]];
[separatorImage setFrame:CGRectMake(99, 4, 5, 71)];
separatorImage.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:separatorImage];
titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(111, 4, 188, 26)];
titleLabel.font = [UIFont fontWithName:#"Arial Rounded MT Bold" size:21.0];
titleLabel.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:titleLabel];
I have a hunch that sending -setSelected:animated: to super at the beginning of the method is preventing the changes to your cell. Try moving the call to super to the end of the method. You should also override -setHighlighted:animated: or your selection will appear to lag.

How to stop the horizontal scrolling programmatically?

I have a UITextView *textView in cocos2d's CCLayer. The text is scrolling in both the horizontal and vertical directions. But, I need it to scroll and bounce only in vertically.
How to stop the horizontal scrolling programmatically ?
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(100,200, windowSize.height/2,windowSize.width/2)];
textView.backgroundColor = [UIColor clearColor];
textView.text = #"I am First enemy I am First enemy I am First enemy I am First enemy I am First enemy I am First enemy I am First enemy I am First enemy";
[textView setEditable:NO];
textView.font = [UIFont fontWithName:#"Helvetica" size:24.0f];
CGPoint location = CGPointMake(200, 160);
textView.showsHorizontalScrollIndicator = NO;
//textView.bounces = NO;
//textView.alwaysBounceVertical = YES;
textView.center = location;
textView.transform = CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS( 90.0f ));
What should I do stop scrolling horizontally ?
Thank You.
I don't know whether it was the good solution. But it worked for me. I am posting here so that any one may correct it.
textView = [[UITextView alloc] initWithFrame:CGRectMake(100,200,200 ,200)];
textView.backgroundColor = [UIColor clearColor];
NSLog(#"description: %#", enemyDescription);
textView.text = enemyDescription;
[textView setEditable:NO];
textView.font = [UIFont fontWithName:#"Helvetica" size:14.0f];
CGPoint location = CGPointMake(200, 160);
textView.showsHorizontalScrollIndicator = NO;
textView.alwaysBounceVertical = YES;
textView.transform = CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS( 90.0f ));
[[[CCDirector sharedDirector]openGLView]addSubview:textView];
It worked for me. It scrolls only up and down and also bounces up and down.
Thank You.