I want when tab on row a UIView containing two button appear at the center of the cell, so I did the following code
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//how can I get the text of the cell here?
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIView *v = [[UIView alloc] init];
v.center = cell.center;
UIButton*button1 =[[UIButton alloc] init];
button1.frame = CGRectMake(v.center.x - 5 , v.center.y , 5 , v.center.y + 4 );
button1.titleLabel.text = #"I'm label 1";
[v addSubview: button1];
UIButton*button2 =[[UIButton alloc] init];
button2.frame = CGRectMake(v.center.x - + 1 , v.center.y , 5 , v.center.y + 4 );
button2.titleLabel.text = #"I'm label 2";
[v addSubview: button2];
[cell addSubview:v];
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
}
UIView doesn't appear. How to solve that?
try:
[cell.contentView addSubview: v];
It should work.
Edit:
Try this code. Give the frames accordingly, it will work.
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(44, 100, 200, 200)];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0, 0, 50, 50);
[v addSubview:button];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell.contentView addSubview:v];
UIView *v = [[UIView alloc] init];//You have not set the frame of the v;
initWithFrame is the designated initialiser for views.
Since you are using init it doesn't know how large a view to create.
Also UIButton *button1 = [[UIButton alloc] init]; is incorrect. UIButtons are created with the buttonWithType: class method.
Related
I have an application with navigation controller and some table view controller. In table view controller I have a two section header my definitions:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (section == 0) {
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 74)];
UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"toolbarTopBack.png"]];
UILabel *headline = [[UILabel alloc] initWithFrame:CGRectMake(11, 14, 305, 21)];
headline.backgroundColor = [UIColor clearColor];
headline.textColor = [UIColor whiteColor];
headline.font = [UIFont fontWithName:#"Helvetica Neue" size:21];
headline.text = searchPosition;
UILabel *subHeadline = [[UILabel alloc] initWithFrame:CGRectMake(11, 36, 305, 21)];
subHeadline.backgroundColor = [UIColor clearColor];
subHeadline.textColor = [UIColor grayColor];
subHeadline.font = [UIFont fontWithName:#"Helvetica Neue" size:16];
subHeadline.text = searchRegion;
[customView addSubview:myImageView];
[customView addSubview:headline];
[customView addSubview:subHeadline];
return customView;
} else {
// create the parent view that will hold header Label
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 44)];
customView.userInteractionEnabled = YES;
UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"mainToolBar.png"]];
UIToolbar *topToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 44)];
topToolbar.barStyle = UIBarStyleDefault;
[topToolbar setBackgroundColor:[UIColor clearColor]];
static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{
NSMutableDictionary *appSettingsData = [[NSMutableDictionary alloc] initWithDictionary:[appDelegate getAppStrings]];
NSArray *segmentItems = [NSArray arrayWithObjects:[NSString stringWithFormat:#"%# (%d)", [appSettingsData valueForKey:#"segmentButton4"], listCountOffers], [NSString stringWithFormat:#"%# (%d)", [appSettingsData valueForKey:#"segmentButton5"], [[appDelegate comunication] getSimilarCount:[appDelegate getCurrentCI] idPosition:idPosition idRegion:idRegion]], nil];
segmentControl = [[UISegmentedControl alloc] initWithItems:segmentItems];
segmentControl.frame = CGRectMake(6, 8, 308, 29);
segmentControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
segmentControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segmentControl setTintColor:[UIColor grayColor]];
[segmentControl addTarget:self action:#selector(segmentedControlIndexChanged:) forControlEvents:UIControlEventValueChanged];
segmentControl.momentary = NO;
segmentControl.selectedSegmentIndex = 0;
});
UIBarButtonItem *toolbarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentControl];
[topToolbar addSubview:toolbarItem.customView];
[customView addSubview:myImageView];
[customView addSubview:topToolbar];
return customView;
}
}
I use "static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{", because I need create this header only first time (because when I scroll table, method is call and call ... and this is wrong) ...
Everything works fine, when I create table view controller it show a headers, when I scroll it, nothing is recreating (it is fine), but when I push back button a then reopen tableview controller headers are empty. Where is problem ? Is there any solution, how to fix it ? Thanks a lot
Well, to me it looks like it's a problem with your static dispatch.
When you push the Back button, chances are that your view holding the table view is released (I don't know your code, but I suppose it is like that), meaning all internal variables are gone - except your static dispatch, which won't be called the next time you instantiate the view. So, during the next instantiation your segmentItems will not be created, but because the view was released, they are empty. You should solve your 'only create once' problem differently, e.g. by remembering the created segmentItems in a dictionary and getting them from there if they do not exist yet.
You're using dispatch_once without really needing to. The block will only be executed once, even if you subsequently remove the view controller from memory and deallocate segmentControl.
Use lazy loading instead - create a property for your segmentControl view within your view controller, and in the accessor for that, if the backing ivar is nil, create it then:
Your synthesize statement:
#synthesize segmentControl = _segmentControl
Your accessor method:
-(UISegmentedControl*)segmentControl
{
if (_segmentControl)
return _segmentControl;
UISegmentedControl *segmentControl = //... create your control here
self.segmentControl = segmentControl
return segmentControl;
}
Then when you want to use the view, use self.segmentControl. The first time you call it, it will be created, the subsequent times, it will be re-used.
check it with breakpoint and see the process. it maybe the memory allocation problem. See this it may help you. http://www.icodeblog.com/2010/12/10/implementing-uitableview-sections-from-an-nsarray-of-nsdictionary-objects/
I need a UITableView whose first row should show a logo, while other rows should show the other table datas.
I tried the following code:
if(indexPath.row == 0) {
//Code for imageview
} else {
//Code to display array content
}
My problem is that I don't get the first array item in the table view.
Edited::
if (indexPath.row == 0) {
CGRect myImageRect = CGRectMake(120.0f, 5.0f, 70.0f, 55.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:#"logo.png"]];
[cell.contentView addSubview:myImage];
}
else {
NSDictionary *dict = [menuitems objectAtIndex:indexPath.row];
cell.textLabel.text =[dict objectForKey:#"category"];
}
Try to use an headerview. Use the following code to place logo at the top of row.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
UIImageView *myimgvw = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
[myimgvw setImage:[UIImage imageNamed:#"logo.png"]];
[myView addSubview:myimgvw];
return myView;
}
You haven't shown your code that displays the array content, but I guess you are using something like
[dataArray objectAtIndex:indexPath.row];
But because you are showing the image at row 0, you need to offset this by -1. In other words, the second row in your table will have indexPath.row = 1, but you need that to be objectAtIndex:0.
Then you need to put use indexPath-1 to show text instead of indexPath in else part of above code snippet.
I'm trying to add a view behind my grouped UITableView. Unfortunately, whenever I scroll, the background view moves as well. This happens even when I use [self.view insertSubview:backgroundView belowSubview:_tableView], or [_tableView setBackgroundView:backgroundView]. Why is this background view scrolling? Also, why does my tableView scroll, even though I have disabled scrolling? Are these related?
In app Delegate:
History *historyController = [[History alloc] init];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:historyController];
In History.m:
- (void)viewDidLoad {
CGRect frame = self.view.frame;
frame.origin.x = 0;
frame.origin.y = 0;
_tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
_tableView.delegate = self;
_tableView.dataSource = self;
[_tableView setScrollEnabled:NO];
[_tableView setBackgroundColor:[UIColor clearColor]];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
UIView *bg = [[UIView alloc] initWithFrame:frame];
[bg setBackgroundColor:[UIColor scrollViewTexturedBackgroundColor]];
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"CenterFade.png"]];
iv.alpha = .750;
[bg addSubview:iv];
[iv release]; iv = nil;
[self.view addSubview:bg];
[self.view addSubview:_tableView];
[bg release]; bg = nil;
[_tableView release];
[super viewDidLoad];
}
Try
- (void)viewDidLoad {
CGRect frame = self.view.frame;
frame.origin.x = 0;
frame.origin.y = 0;
UIView *bg = [[UIView alloc] initWithFrame:frame];
[bg setBackgroundColor:[UIColor scrollViewTexturedBackgroundColor]];
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"CenterFade.png"]];
iv.alpha = .750;
[bg addSubview:iv];
[iv release];
iv = nil;
_tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
_tableView.delegate = self;
_tableView.dataSource = self;
[_tableView setScrollEnabled:NO];
[_tableView setBackgroundColor:[UIColor clearColor]];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[bg addSubview:_tableView];
[_tableView release];
_tableView = nil;
[self.view addSubview:bg];
[bg release];
bg = nil;
[super viewDidLoad];
}
If your controller is an UITableViewController just change it to an UIViewController<UITableViewDelegate, UITableViewDatasource> with a tableView property (It's the same)
I don't know why this is happening, but for some reason self.view is a UITableView and not a UIView. Creating a new UIView and setting it to self.view fixed the problem. I am not using a UITableViewController, but a UIViewController. No idea where the UITableView is coming from!
set [_tableView setBackgroundColor:[UIColor clearColor]];
and then put your background view under UITableView in UIViewController view hierarchy.
UITableView has a backgroundView property.
Swift:
var backgroundView: UIView?
Objective-C
#property(nonatomic, readwrite, retain) UIView *backgroundView
This view will not be moved when you scroll the tableView
In my iphone app, I first call a controller which stack up a few view controllers based on index. This is done cause based on the user's selection, i will need to show different screens (basically, i have a welcome screen, tabbar view - which is the main app, sign in and sign up pages). As of now, everything works perfectly - I stack them up and remove / switch based on the need. The problem is that i would like to add a nav bar to both the sign in and sign up views. However, when i do that, something weird happens - i see a nav bar, but on TOP of it, there is a white bar as well (half width more or less). How can i add this uinavbar successfully?
Here is the controller that is being called by the AppDelegate:
- (void)viewDidLoad
{
[super viewDidLoad];
myTabBarVC = [[tabBarController alloc] initWithNibName:#"tabBarController" bundle:nil];
[self.view insertSubview:myTabBarVC.view atIndex:0];
myLoginVC = [[loginViewController alloc] initWithNibName:#"loginViewController" bundle:nil];
[self.view insertSubview:myLoginVC.view atIndex:1];
mySignUp = [[SignUpView alloc] initWithNibName:#"SignUpView" bundle:nil];
[self.view insertSubview:mySignUp.view atIndex:2];
myWelcomeView = [[WelcomeView alloc] initWithNibName:#"WelcomeView" bundle:nil];
[self.view insertSubview:myWelcomeView.view atIndex:3];
}
When I add that to one of the view controllers in this same method, it doesnt work, as described above.
myLoginVC = [[loginViewController alloc] initWithNibName:#"loginViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myLoginVC];
[self.view insertSubview:navController.view atIndex:1];
How can I make it work? Please help. Thanks!
ADDING MORE INFO ABOUT THE VIEW WHERE THE KEYBOARD IS ON BY DEFAULT:
- (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:#"Cell"];
cell.textLabel.text = [[NSArray arrayWithObjects:#"",#"",nil]
objectAtIndex:indexPath.row];
if (indexPath.row == 0) {
textField1 = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 280, 21)];
textField1.delegate = self;
textField1.placeholder = #"example#EMAIL.com";
textField1.text = [inputTexts objectAtIndex:indexPath.row/2];
textField1.tag = indexPath.row/2;
cell.accessoryView = textField1;
textField1.returnKeyType = UIReturnKeyNext;
textField1.clearButtonMode = UITextFieldViewModeWhileEditing ;
[textField1 becomeFirstResponder];
textField1.tag = 1;
}
else
{
textField2 = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 280, 21)];
textField2.delegate = self;
textField2.placeholder = #"password";
textField2.text = [inputTexts objectAtIndex:indexPath.row/2];
textField2.tag = indexPath.row/2;
textField2.returnKeyType = UIReturnKeyDone;
textField2.clearButtonMode = UITextFieldViewModeWhileEditing;
cell.accessoryView = textField2;
[textField1 becomeFirstResponder];
textField1.tag = 2;
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
I don't think you should be adding navController.view as a subview to your view controller. I suspect that's a source of the trouble. Instead use a UINavigationBar object.
With regards to your second question, if it was a question and not a remark, about the keyboard being by default, that happens because you set your textField1 as first responder.
I have a main table view and a detail view. when i click cell detail view should show the details of that item. now what i wanna do is to create subview and put that subview in detailView instead of creating a detailview for every cell. it ll solve my some other problems. Code looks like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *nextController = [[DetailViewController alloc] init];
int storyIndex = [indexPath indexAtPosition:[indexPath length] -1];
[nextController initWithObjectAtIndex:storyIndex inArray:stories];
NSString *storyTitle = [[stories objectAtIndex:storyIndex] objectForKey:#"title"];
nextController.title = #"Details";
UIBarButtonItem *tempButtonItem = [[[UIBarButtonItem alloc] init] autorelease];
tempButtonItem.title = #"Tillbaka";
self.navigationItem.backBarButtonItem = tempButtonItem ;
[self.navigationController pushViewController:nextController animated:YES];
[nextController release];
}
now how can i create a subview with a label and change text of that label to storyTitle.
thanx in advance.
how can i create a subview with a label and change text of that label to storyTitle
Given a parent view, you don't need to create a new subview just to add a label to the parent. But, assuming you really do want to add a subview with a label to your parent view, then do this:
...
UIView *parentView;
...
UIView *subView = [[[UIView alloc] initWithFrame:mySubViewFrame] autorelease];
UILabel *lbl = [[[UILabel alloc] initWithFrame:myLabelFrame] autorelease];
lbl.text = storyTitle;
[subView addSubview: lbl];
[parentView addSubview: subView];