Why do I have this weird spacing - objective-c

I can not get rid of this strange spacing between line 0 and 1.
I want the same spacing between lines 0 and 1 as I have between line 1 and 2.
[self.tableView setBackgroundColor:TABLE_VIEW_BACKGROUND_COLOR];
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 250)];
UIImageView *headerBackgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"addres_box"]];
[headerBackgroundImageView setFrame:CGRectMake(headerView.frame.origin.x + 10, headerView.frame.origin.y + 10, headerView.frame.size.width - 20, 160)];
[headerView addSubview:headerBackgroundImageView];
UILabel *headerTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(headerBackgroundImageView.frame.origin.x + 10, headerBackgroundImageView.frame.origin.y + 10, headerBackgroundImageView.frame.size.width - 20, 20)];
[headerTextLabel setBackgroundColor:[UIColor clearColor]];
[headerTextLabel setFont:[UIFont boldSystemFontOfSize:headerTextLabel.font.pointSize]];
[headerTextLabel setTextColor:[UIColor whiteColor]];
[headerTextLabel setLineBreakMode:UILineBreakModeTailTruncation];
[headerTextLabel setNumberOfLines:0];
NSString *headerTextString = #"0";
NSString *detailTextString = #"1\n2";
[headerTextLabel setText:headerTextString];
[headerView addSubview:headerTextLabel];
_headerTextLabel = headerTextLabel;
UILabel *headerDetailTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(headerTextLabel.frame.origin.x, headerTextLabel.frame.origin.y+20, headerTextLabel.frame.size.width, 140)];
[headerDetailTextLabel setBackgroundColor:[UIColor clearColor]];
[headerDetailTextLabel setTextColor:[UIColor whiteColor]];
[headerDetailTextLabel setLineBreakMode:UILineBreakModeTailTruncation];
[headerDetailTextLabel setNumberOfLines:0];
[headerDetailTextLabel setText:detailTextString];
[headerView addSubview:headerDetailTextLabel];
_headerDetailTextLabel = headerDetailTextLabel;
[self.tableView setTableHeaderView:headerView];
[self.tableView setSeparatorColor:TABLE_VIEW_SEPARATOR_COLOR];
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
I hope someone can help me!

Perhaps your headerDetailTextLabel is centered vertically, try adding more lines like "1\n2\n3\n4" and see the result.

Thanks for the hints, I solved it by adding "sizeToFit" and now every things look good
[headerDetailTextLabel setText:detailTextString];
[headerDetailTextLabel sizeToFit]; //this line fixed the spacing problem
[headerView addSubview:headerDetailTextLabel];

Related

White line on the bottom on ios 7 with UITextField

I have text field, i need to show the cursor and hide the keyboard. It looks ok in the IOS 6 and IOS 5. But in IOS 7 i can see small white line on the bottom.
My code is
UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
[scrollview setDelegate:self];
[self.view addSubview:scrollview];
[self.view bringSubviewToFront:scrollview];
[scrollview setBackgroundColor:[UIColor blackColor]];
scrollview.contentSize = CGSizeMake(768, 1024);
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 500, 100)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.returnKeyType = UIReturnKeyDone;
[textField setUserInteractionEnabled:YES];
[textField setEnabled:YES];
[scrollview addSubview:textField];
// Here I need to show the cursor without showing keyboard
UIView* dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
((UITextField*)textField).inputView = dummyView;
[((UITextField*)textField) becomeFirstResponder];
Anyone having any thought how to fix this?
I think this is due to the keyboard view, whose having the height as 1.
So if you change the line
UIView* dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
to
UIView* dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
it will work.

Altering selected state for UITableViewCell

I've got a standard UITableView where each cell is a custom UIView. Everything works fine, except when the cell is selected, it turns blue (as expected) but the UIView is hidden!
How can I change the contents of the view to be visible when the cell is selected?
- (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];
}
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 100)];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 300, 40)];
[headerLabel setText:[[[todayCollection events]objectAtIndex:indexPath.row]name]];
[headerLabel setFont:[UIFont boldSystemFontOfSize:22]];
headerLabel.highlightedTextColor = [UIColor whiteColor];
UILabel *venueLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 40, 300, 20)];
[venueLabel setText:[[[[todayCollection events]objectAtIndex:indexPath.row]venue]name]];
[venueLabel setFont:[UIFont italicSystemFontOfSize:16]];
LBVenueBadge *venueBadge = [[LBVenueBadge alloc] initWithGenre:[[[todayCollection events]objectAtIndex:indexPath.row]genre] frame:CGRectMake(85, 73, 100, 20)];
UIImageView *pinImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 75, 18, 18)];
[pinImage setImage:[UIImage imageNamed:#"193-location-arrow.png"]];
UILabel *distanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 75, 100, 15)];
[distanceLabel setFont:[UIFont systemFontOfSize:12]];
NSString *distanceString = [[[[todayCollection events]objectAtIndex:indexPath.row]venue]distanceString];
if([[[[todayCollection events]objectAtIndex:indexPath.row]performances]count] == 1)
{
UILabel *performanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 75, 50, 15)];
[performanceLabel setText:[[[[[todayCollection events]objectAtIndex:indexPath.row]performances]objectAtIndex:0]time]];
[performanceLabel setBackgroundColor:[UIColor clearColor]];
[performanceLabel setFont:[UIFont systemFontOfSize:12]];
[containerView addSubview:performanceLabel];
}else{
UILabel *performanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 75, 50, 15)];
[performanceLabel setText:[NSString stringWithFormat:#"%i shows", [[[[todayCollection events]objectAtIndex:indexPath.row]performances]count]]];
[performanceLabel setBackgroundColor:[UIColor clearColor]];
[performanceLabel setFont:[UIFont systemFontOfSize:12]];
[containerView addSubview:performanceLabel];
}
[distanceLabel setText:distanceString];
[containerView addSubview:pinImage];
[distanceLabel setBackgroundColor:[UIColor clearColor]];
[containerView addSubview:distanceLabel];
[headerLabel setBackgroundColor:[UIColor clearColor]];
[containerView addSubview:headerLabel];
[venueLabel setBackgroundColor:[UIColor clearColor]];
[containerView addSubview:venueLabel];
[containerView addSubview:venueBadge];
if(indexPath.row % 2 == 0)
{
[containerView setBackgroundColor:[UIColor colorWithRed:0.239 green:0.239 blue:0.232 alpha:0.05]];
}else{
[containerView setBackgroundColor:[UIColor colorWithRed:0.239 green:0.239 blue:0.232 alpha:0.02]];
}
cell.backgroundView = containerView;
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;
}
Thanks,
As the contentView property is readonly, I can add a subview to it instead.
[cell.contentView addSubview:containerView];
You are adding views to the background rather than the contentView of the cell replace this line.
cell.backgroundView = containerView;
with
[cell.contentView addSubview:containerView];

big tableview scrolling lags

i have a performance issue in my tableview
my cellForRow looks like that:
if (tableView == allMonthTable) {
static NSString *cellIdentifier = #"cell";
AllMonthCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[AllMonthCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] ;
}
Month *mo = [[dataHandler allMonth]objectAtIndex:indexPath.section];
double budgetValue = mo.budget.doubleValue;
[cell.budgetLabel setText:[NSString stringWithFormat:#"%.2f",budgetValue ]];
double spentValue = mo.spent.doubleValue;
[cell.spentLabel setText:[NSString stringWithFormat:#"%.2f",spentValue ]];
if (spentValue > 0) {
[cell.spentLabel setText:[NSString stringWithFormat:#"+%.2f",spentValue]];
[cell.spentLabel setTextColor:[self greenColor]];
}
else if (spentValue < 0){
[cell.spentLabel setText:[NSString stringWithFormat:#"%.2f",spentValue]];
[cell.spentLabel setTextColor:[self redColor]];
}
double balanceValue = budgetValue+spentValue;
[cell.balanceLabel setText:[NSString stringWithFormat:#"%.2f",balanceValue ]];
if (balanceValue > 0) {
[cell.balanceLabel setText:[NSString stringWithFormat:#"+%.2f",balanceValue]];
[cell.balanceLabel setTextColor:[self greenColor]];
}
else{
[cell.balanceLabel setText:[NSString stringWithFormat:#"%.2f",balanceValue]];
[cell.balanceLabel setTextColor:[self redColor]];
}
double avgDayValue = mo.avgDay.doubleValue;
[cell.avgDayLabel setText:[NSString stringWithFormat:#"%.2f",avgDayValue ]];
if (avgDayValue > 0) {
[cell.avgDayLabel setText:[NSString stringWithFormat:#"+%.2f",avgDayValue]];
}
int numberOfExpValue = mo.expenses.intValue;
[cell.numberOfExpLabel setText:[NSString stringWithFormat:#"%d",numberOfExpValue ]];
return cell;
}
return nil;
}
and my AllMonthCell.m files looks like that:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
UIFont *font = [UIFont fontWithName:#"Cochin" size:14.0];
UIFont *fontBold = [UIFont fontWithName:#"Cochin-Bold" size:14.0];
self.frame = CGRectMake(0, 0, 312, 100);
UIView *top = [[UIView alloc]initWithFrame:CGRectMake(4, 0, 304, 1)];
[top setBackgroundColor:[UIColor lightGrayColor]];
UILabel *budgetStringLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 102, 16)];
[budgetStringLabel setTextAlignment:UITextAlignmentLeft];
[budgetStringLabel setFont:font];
[budgetStringLabel setTextColor:[UIColor lightGrayColor]];
[budgetStringLabel setText:#"Month Budget:"];
[budgetStringLabel setBackgroundColor:[self grayColor]];
UILabel *spentStringLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 30, 102, 16)];
[spentStringLabel setTextAlignment:UITextAlignmentLeft];
[spentStringLabel setFont:font];
[spentStringLabel setTextColor:[UIColor lightGrayColor]];
[spentStringLabel setText:#"Spent Money:"];
[spentStringLabel setBackgroundColor:[self grayColor]];
UIView *divide1 = [[UIView alloc]initWithFrame:CGRectMake(10, 50, 292, 1)];
[divide1 setBackgroundColor:[UIColor lightGrayColor]];
UILabel *balanceStringLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 55, 102, 16)];
[balanceStringLabel setTextAlignment:UITextAlignmentLeft];
[balanceStringLabel setFont:font];
[balanceStringLabel setTextColor:[UIColor lightGrayColor]];
[balanceStringLabel setText:#"Month Balance:"];
[balanceStringLabel setBackgroundColor:[self grayColor]];
UIView *divide2 = [[UIView alloc]initWithFrame:CGRectMake(10, 74, 292, 1)];
UIView *divide3 = [[UIView alloc]initWithFrame:CGRectMake(10, 76, 292, 1)];
[divide2 setBackgroundColor:[UIColor lightGrayColor]];
[divide3 setBackgroundColor:[UIColor lightGrayColor]];
UILabel *avgDayStringLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 94, 200, 16)];
[avgDayStringLabel setTextAlignment:UITextAlignmentLeft];
[avgDayStringLabel setFont:font];
[avgDayStringLabel setTextColor:[UIColor lightGrayColor]];
[avgDayStringLabel setText:#"Per Day:"];
[avgDayStringLabel setBackgroundColor:[self grayColor]];
UILabel *numberOfExpStringLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 114, 102, 16)];
[numberOfExpStringLabel setTextAlignment:UITextAlignmentLeft];
[numberOfExpStringLabel setFont:font];
[numberOfExpStringLabel setTextColor:[UIColor lightGrayColor]];
[numberOfExpStringLabel setText:#"Expenses Made:"];
[numberOfExpStringLabel setBackgroundColor:[self grayColor]];
[self addSubview:budgetStringLabel];
[self addSubview:top];
[self addSubview:spentStringLabel];
[self addSubview:divide1];
[self addSubview:balanceStringLabel];
[self addSubview:divide2];
[self addSubview:divide3];
[self addSubview:numberOfExpStringLabel];
[self addSubview:avgDayStringLabel];
self.budgetLabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 10, 102, 16)];
[self.budgetLabel setTextAlignment:UITextAlignmentRight];
[self.budgetLabel setFont:font];
[self.budgetLabel setBackgroundColor:[self grayColor]];
[self.budgetLabel setTextColor:[UIColor darkGrayColor]];
self.spentLabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 30, 102, 16)];
[self.spentLabel setTextAlignment:UITextAlignmentRight];
[self.spentLabel setFont:font];
[self.spentLabel setBackgroundColor:[self grayColor]];
[self.spentLabel setTextColor:[UIColor lightGrayColor]];
self.balanceLabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 55, 102, 16)];
[self.balanceLabel setTextAlignment:UITextAlignmentRight];
[self.balanceLabel setFont:fontBold];
[self.balanceLabel setBackgroundColor:[self grayColor]];
self.avgDayLabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 94, 102, 16)];
[self.avgDayLabel setFont:font];
[self.avgDayLabel setBackgroundColor:[self grayColor]];
[self.avgDayLabel setTextColor:[UIColor lightGrayColor]];
[self.avgDayLabel setTextAlignment:UITextAlignmentRight];
self.numberOfExpLabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 114, 102, 16)];
[self.numberOfExpLabel setTextAlignment:UITextAlignmentRight];
[self.numberOfExpLabel setFont:font];
[self.numberOfExpLabel setBackgroundColor:[self grayColor]];
[self.numberOfExpLabel setTextColor:[UIColor lightGrayColor]];
[self addSubview:self.budgetLabel];
[self addSubview:self.spentLabel];
[self addSubview:self.balanceLabel];
[self addSubview:self.numberOfExpLabel];
[self addSubview:self.avgDayLabel];
[self setBackgroundColor:[self grayColor]];
}
return self;
}
here is a screenshot of time profiling while scrolling:
so i create reusable cells of the type AllMonthCell, and assign the values to them, is there a way to make it faster? i have pretty serious lags while scrolling the table..
a hint would be great :)
You should reuse the cells
Let me derive you a pattern
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"cell";
UILabel *noExpense;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] autorelease];
//Just the view alone should be created here no assigning of data here
//let me add just one custom view for easy understanding
noExpense = [[UILabel alloc]initWithFrame:CGRectMake(8, 20, 296, 16)];
[noExpense setTextAlignment:UITextAlignmentCenter];
[noExpense setFont:fontName];
//[noExpense setText:#"No data available yet."]; Dont do this
[noExpense setTextColor:[UIColor lightGrayColor]];
[noExpense setBackgroundColor:tableView.backgroundColor];
[noExpense setTag:100]; //Must set tag here
[cell addSubview:noExpense];
}
else
{
noExpense = [cell viewWithTag:100];
//You have to initialize your custom views which you created already.
//Just the view alone should be assigned here no assigning of data here
}
//Load all the data into the views here
[noExpense setText:#"Somedata which you have to set"];
return cell;
}
To know more about tableview see the doc. Its the bible for understanding tableviews.
http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7

How can I read the coordinates of a button?

UIView *contentView = [[UIView alloc] initWithFrame: [[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"background.jpg"]];
self.view = contentView;
[contentView release];
[contentView addSubview:scrollView];
scrollView.contentSize = CGSizeMake(1800,480);
UIScrollView* tscrollView = [[UIScrollView alloc] initWithFrame:(CGRect){.origin.x = 0.0f, .origin.y = 0.0f, .size.width = 320.0f, .size.height = 5480.0f}];
tscrollView.contentSize = CGSizeMake(1800,192);
Object1 = [UIButton buttonWithType:1];
Object1.frame = CGRectMake(383, 250, 256, 192);
UIImage *buttonImage = [UIImage imageNamed:#"02_1024_768.jpg"];
[Object1 setBackgroundImage:buttonImage forState:UIControlStateNormal];
//[self.view addSubview:Object1];
[scrollView addSubview:Object1];
Object5 = [UIButton buttonWithType:1];
Object5.frame = CGRectMake(1487, 250, 256, 192);
UIImage *buttonImage1 = [UIImage imageNamed:#"p5.jpg"];
[Object5 setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
[scrollView addSubview:Object5];
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(400, 100, 200, 200)];
[myLabel setBackgroundColor:[UIColor clearColor]];
myLabel.text = #"Button1 is in range";
[myLabel setTextAlignment:UITextAlignmentCenter];
[myLabel setTextColor:[UIColor whiteColor]];
NSLog(#"x = %d",Object1.center.x);
position = CGPointMake(0,0);
Object1.center = CGPointMake(Object1.center.x+position.x,Object1.center.y+position.y);
if((Object1.center.x >341) && (Object1.center.x < 597)){
[myLabel setHidden:NO];
}
else {
[myLabel setHidden:YES];
}
[self.view addSubview:myLabel];
Here is the code, but seems that the x coordinates always equals to 0. Could some1 tell me why and how to fix it? Thanks in advance.
Try this
NSLog(#"x=%f",Object1.center.x);
coordinates are float value and you print like int so you get 0. i check your code in project

Changing value on a label with UISlider control in WePopoverControler

I am in a process of implementing WePopovercontroller in my app and I have a question on how to implement uislider on it. I am able to show the slider in the UIView and get the action when slider moved but not sure how to set the value of the label in that view. Here is the part of the code
-(void)popoverSliderMoved:(UISlider *) sender{
NSLog(#"slider %f",sender.value);
}
-(IBAction)showSettingsMenu:(UIButton *)sender{
if(!self.popoverSettingsController) {
// Create a label and button for the popover
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
[label setText:#"Bookmark it!"];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setTextAlignment:UITextAlignmentCenter];
UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(0, 45, 100, 40)];
[slider addTarget:self action:#selector(popoverSliderMoved:) forControlEvents:UIControlEventValueChanged];
UIFont *font = [UIFont boldSystemFontOfSize:20];
[label setFont:font];
CGSize size = [label.text sizeWithFont:font];
CGRect frame = CGRectMake(0, 0, size.width + 10, size.height + 10); // add a bit of a border around the text
label.frame = frame;
UIButton *button = [[UIButton alloc] initWithFrame:label.frame];
[button addSubview:label];
[button addTarget:self action:#selector(popoverButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIViewController *viewCon = [[UIViewController alloc] init];
[viewCon.view addSubview:slider];
[viewCon.view addSubview:button];
CGRect popOverFrm = CGRectMake(0, 0, frame.size.width + slider.frame.size.width, frame.size.height + slider.frame.size.height);
viewCon.contentSizeForViewInPopover = popOverFrm.size;//CGSizeMake(100, 36);
NSLog(#"Label Frame: %#", NSStringFromCGRect(label.frame));
NSLog(#"Popover size: %#", NSStringFromCGSize(viewCon.contentSizeForViewInPopover));
NSLog(#"ViewCon: %#", NSStringFromCGRect(viewCon.view.frame));
self.popoverSettingsController = [[WEPopoverController alloc] initWithContentViewController:viewCon];
//[self.popoverSettingsController setDelegate:self];
}
if([self.popoverSettingsController isPopoverVisible]) {
[self.popoverSettingsController dismissPopoverAnimated:YES];
// [navPopover setDelegate:nil];
self.popoverSettingsController = nil;
} else {
[self.popoverSettingsController presentPopoverFromRect:CGRectMake(sender.frame.size.width, 0, 200, 57)
inView:self.navigationController.view
permittedArrowDirections:UIPopoverArrowDirectionUp | UIPopoverArrowDirectionDown
animated:YES];
}
}
Would i need to use a delegate in this case or a property for this viewcontroller to set the label.
Thanks!!!
Yes using a property would be what I would do.