I am trying to achieve this:
but i get this:
I have a view cotroller with a view table on it
This is the interface:
#interface LoginViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UITableView *tblCredentials;
#end
This is the implementation:
#interface LoginViewController ()
#end
#implementation LoginViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated
{
self.tblCredentials.delegate=self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UITextField *textField = [[UITextField alloc] init];
textField.enablesReturnKeyAutomatically = YES;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
CGRect cellBounds = cell.bounds;
CGFloat textFieldBorder = 10.f;
CGRect aRect = CGRectMake(textFieldBorder, 9.f, CGRectGetWidth(cellBounds)-(2*textFieldBorder), 31.f );
textField.frame = aRect;
if(indexPath.row==0)
{
textField.placeholder = #"Username";
textField.returnKeyType = UIReturnKeyNext;
textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
}
else
{
textField.placeholder = #"Password";
textField.returnKeyType = UIReturnKeyDone;
textField.secureTextEntry = YES;
}
[cell.contentView addSubview:textField];
return cell;
}
#end
I put a breakpoint on the in the cellForRowAtIndexPath and it doesn't stop there, so those text fields don't get rendered.
What am I missing?
PS: Is this a bad approach to achieve the goal? (those two grouped text fields)
LE: I am using stroyboard with no xib files
In viewDidLoad, you must set the delegate and call [self.tblCredentials reloadData] in order for the table view to actually "load its data"
You need to create a Custom Table View cell. have a look at this github link.
You're setting the delegate of the table view, but not the datasource, which is where the number of rows etc. comes from.
You're also setting the delegate a bit late in the cycle. Since this is in a xib, why not set the delegate and datasource in the xib instead of in code? If you declare that your view controller conforms to the delegate and data source properties in the header, you will be able to make the connection in IB. If you insist on setting it in code, it should be in viewDidLoad.
Set delegate and dataSource in -viewDidLoad and put [self.tblCredentials reloadData] in -viewWillAppear:.
- (void)viewDidLoad
{
[super viewDidLoad];
self.tblCredentials.delegate=self;
self.tblCredentials.dataSource=self;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated]; // BTW, it's better to call super's -viewWillAppear: here, according to apple's documentation.
[self.tblCredentials reloadData];
}
Related
I have a static table view with 2 cells (in 2 groups).
I have a custom cell (xib) with picture and some data inside.
I've declared through storyboard the cell to be a type of my custom cell and I have a function in my view that sets the photo of the cell and it's data. The problem is that the cell stays empty. I even tried reload data to the table. Whatam I missing? (I'm new to storyboars and viewes)
the code i'm using:
#interface CAUploadDetailsViewController ()
#property (weak, nonatomic) IBOutlet CAContentCell *currentUploadedFile;
#property (strong, nonatomic) IBOutlet UITableView *table;
#end
#implementation CAUploadDetailsViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// self.tableView.delegate = self;
CAContent* content = [[CAContent new] init];
content.name = #"file.txt";
content.sizeInBytes = 4444;
self.currentUploadedFile.item = content;
// Do any additional setup after loading the view.
// self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"ExplorerBackground3"]];
[self.tableView reloadData];
}
- (void)viewWillAppear:(BOOL)animated
{
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#end
when i set the item, it has the following code:
- (void)setItem:(id)item
{
_item = item;
// Cast content
CAContent* content = (CAContent *)item;
self.labelTitle.text = content.name;
NSString* icon = #"MyIcon";
// Set icon image
self.imageIcon.image = [UIImage imageNamed:icon];
}
=== UPDATE (25/1/15)===
i've managed to solve it using cellForRowAtIndexPath, but it feels that this solution is a bit weird (i don't like the code) - is this is how custom cells should be handled? because i need only 1 cell to be custom (i've got 2 cells in my table)
- (void)viewDidLoad
{
[super viewDidLoad];
// self.tableView.delegate = self;
// Customize background
self.view.layer.contents = (id)[UIImage imageNamed:#"ExplorerBackground3"].CGImage;
// Register cell nib
[self.table registerNib:[UINib nibWithNibName:#"CAContentCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:#"CAContentCell"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// we should configure only the first cell.
if (indexPath.section == 0 && indexPath.row == 0)
{
CAContentCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CAContentCell"];
if (cell == nil) {
cell = [[NSBundle mainBundle] loadNibNamed:#"CAContentCell"
owner:self
options:nil][0];
}
CAContent* content = [[CAContent new] init];
content.name = #"file.txt";
content.sizeInBytes = 4444;
cell.item = content;
return cell;
}
return [super tableView:tableView cellForRowAtIndexPath:indexPath];
}
I face this kind of problem for the first time.
I have UITableView on my ViewController and I choosed Grouped style in the IB.
So when I run my app on iPad, it is grouped style and everything is OK, but sometimes UITableView's style becomes Plain. I dont change it anywhere in code or something, it just changes it by itself.
.h
#property (nonatomic, retain) IBOutlet UITableView *myTableView;
.m
myTableView.backgroundColor = [UIColor clearColor];
myTableView.opaque = NO;
myTableView.backgroundView = nil;
I tried to delete the XIB and create a new one, but its still the same problem.
Any ideas?
UPD:
OK, I dont know how, but I got 2 same xibs in my projects with same name. And in one xib I had Plain Style and in the 2nd one I had Grouped style; so its explains why sometimes I had Grouped and somteimes Plain Style. I just deleted one of them and it fixed the problem.
UITableView style doesn't change automatically, unless you change the style either in xib or in code. Please check your code once carefully weather u are changing the style or not and also make sure that u are properly connected datasource and delegate.
Hear is the another way you can create one in the code instead in the xib.
Create the tableview in code following code gives u some idea.
#interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UITableView *aTableVIew;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//Do any additional setup after loading the view, typically from a nib.
aTableVIew = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
aTableVIew.dataSource = self;
aTableVIew.delegate = self;
[self.view addSubview:aTableVIew];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
//Dispose of any resources that can be recreated.
}
-(void)dealloc
{
[aTableVIew release];
[super dealloc];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [aTableVIew dequeueReusableCellWithIdentifier:#"cell"];
if(cell == nil)
{
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"]autorelease];
}
if(indexPath.section == 0)
{
cell.textLabel.text = #"Hello";
}
else if (indexPath.section == 1)
{
cell.textLabel.text = #"World";
}
else
{
cell.textLabel.text = #"Happy coding";
}
return cell;
}
#end
Created an empty xib.
Created a class that overrides UITableViewCell (UserCell).
Put in a custom pieces of logic that simply format some strings for display and connected the two labels in IB.
My view controller owns an UITableView. I've set the view controller as the data source.
Here's how I go about populating it with my custom cells.
(did: http://www.highoncoding.com/Videos/823_Creating_a_Custom_UITableViewCell.aspx)
-(UserCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UserCell* cell = [tableView dequeueReusableCellWithIdentifier:#"Users Table"];
if (!cell) {
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"UserCell" owner:nil options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UserCell class]]) {
cell = (UserCell*) currentObject;
break;
}
}
}
[cell setUserNameText:[[[_userDataManager getUsers] objectAtIndex:indexPath.row] name ]];
[cell setNumberLabelText:indexPath.row];
return cell;
}
For some reason, I am not able to scroll. In viewDidLoad: I'm printing scrollEnabled on my table view. It's "1".
Haven't had this trouble until I tried putting in a custom UITableViewCell. :(
Thanks SO for any advice! :D
EDIT: Custom cell code.
#import "UserCell.h"
#implementation UserCell
#synthesize textLabel, numberLabel;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
-(void) setNumberLabelText:(NSInteger)text {
self.numberLabel.text = [[NSString alloc] initWithFormat:#"# %d", text];
}
-(void) setUserNameText:(NSString*)text {
self.userNameLabel.text = text;
}
#end
Closing this question as answered (I'm a derp sometimes)
I haven't done much iOS stuff using a trackpad. Seems like I was expecting two finger scrolling to "scroll" on the simulator. It does not!
A heads up for anyone else having this issue.
This is similar to my previous question, but my previous question was actually somewhat answered, and this is kind of an extension of it.
I have a UITableView that I am putting into and out of editing mode. While in editing mode, I display an additional row with the insert icon. All other rows, from my data source, are displayed with a delete icon.
For some reason, the last row from my data source is not animating the same as all the other rows, when leaving editing mode. This looks like some obvious code issue to me, based on the behavior, but I can't seem to fix it. I made an animated GIF to show you what I'm talking about:
Does anybody know why this might be happening? It's always that last row from my data source. The custom insert row's icon animates out just fine. It's not a ton of code, but it's easiest to view it all at once, so I will link you to the exact code on Github:
https://github.com/ryancole/pound-client/blob/master/pound-client/controllers/ChannelListViewController.m#L100
It seems like the issue is not with the tableView or any code associated with that. It seems like it is causing because of the pull to refresh view. Unfortunately I don't have the code for that component. I tried your with out the pull to refresh and it is working perfectly. So please remove the pull to refresh and see what is happening.
#import <UIKit/UIKit.h>
#interface zViewController : UIViewController
#property (weak, nonatomic) IBOutlet UITableView *table;
#end
#import "zViewController.h"
#interface zViewController ()
#property (nonatomic, retain) NSArray* data;
#end
#implementation zViewController{
BOOL _editing;
}
#synthesize data = _data;
#synthesize table = _table;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
_editing = NO;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Edit" style:UIBarButtonItemStyleBordered target:self action:#selector(editButtonWasPressed:)];
[self loadRecords];
}
- (void)editButtonWasPressed:(id)sender {
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(saveButtonWasPressed:)];
[self setEditing:YES animated:YES];
}
- (void)saveButtonWasPressed:(id)sender {
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Edit" style:UIBarButtonItemStyleBordered target:self action:#selector(editButtonWasPressed:)];
[self setEditing:NO animated:YES];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
_editing = editing;
if (editing == YES) {
[self.table insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:self.data.count -1 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
}
[super setEditing:editing animated:animated];
[self.table setEditing:editing animated:animated];
if (editing == NO) {
[self.table deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:self.data.count inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
}
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
[self fetchChannels];
}
- (void)fetchChannels {
[self.table reloadData];
}
-(void) loadRecords{
self.data = #[#"Record 1", #"Record 2", #"Record 3"];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return (indexPath.row == self.data.count)? UITableViewCellEditingStyleInsert : UITableViewCellEditingStyleDelete;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _editing? self.data.count + 1 : self.data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = self.data[indexPath.row];
return cell;
}
#end
The animation is correct. You are inserting/deleting last row in -setEditing:animated:, so there are two kinds of animations are going on.
I am having a weird problem and i couldn`t find a solution (or something similar).
The thing is, my UITableView Populates with initial info (for testing), but no matter what i do i can't seem to put it to grouped style (i can select it on the UI but it wont show)
I initially started a TabBar project and added a third navigationController view in the tabs.
#import <UIKit/UIKit.h>
#interface RootViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> {
NSMutableArray *tableData;
}
#property (nonatomic, retain) NSMutableArray *tableData;
-(void)initTableData;
#end
This is the header, and as you can see it has nothing out of the ordinary. The following code is inside the .m file of the header i just posted(ill only be posting uncommented code:
#synthesize tableData;
-(void)initTableData
{
tableData = [[NSMutableArray alloc] init];
[tableData addObject:#"Cidade"];
[tableData addObject:#"Veículo"];
[tableData addObject:#"Ano"];
[tableData addObject:#"Valor"];
[tableData addObject:#"Cor"];
[tableData addObject:#"Combustível"];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Busca";
UIBarButtonItem *_backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.backBarButtonItem = _backButton;
[self initTableData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 6;
}
// Customize the appearance of table view cells.
- (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] autorelease];
}
// Configure the cell...
cell.textLabel.text = [tableData objectAtIndex:[indexPath row]];
return cell;
}
- (void)dealloc {
[tableData release];
[super dealloc];
}
Nothing out of the ordinary again as you can see...
Any idea of what may be causing this? I tried
- (id)initWithStyle:(UITableViewStyle)style {
// Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
// Custom initialization.
}
return self;
}
because i don't know what else to do. (also didn't worked)
Again, i got the delegate and datasource set to File`s Owner.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Set the tab bar controller as the window's root view controller and display.
self.window.rootViewController = self.tabBarController;
RootViewController *rvc = [[RootViewController alloc] initWithStyle: UITableViewStyleGrouped];
[self.window makeKeyAndVisible];
return YES;
}
As you have modified the - (id)initWithStyle:(UITableViewStyle)style initializer to return a UITableView with a grouped style, do you call this initializer when you initialize the RootViewController?
RootViewController *rvc = [[RootViewController] alloc] initWithStyle: UITableViewStyleGrouped];
Grouped tables respond to the sections. You only have 1 section listed so you will only see the 1 group. Try and add a 2nd tableData for the 2nd group and return 2 sections. You will also have to split the data in your -cellForRowAtIndexPath by section as well to make sure the data goes to the right section.
if (indexpath.section == 0) {
// first section and first tableData
}
if (indexpath.section == 1) {
// second section and second tableData
}