I am following this tutorial: http://www.appcoda.com/ios-programming-tutorial-create-a-simple-table-view-app/ which shows how to create an UITableView and populate it programmatically. All's good, it works.
Now, I am trying to make the table fill the screen: regardless of the device or orientation. I did this:
But now the table view simply does not appear in my screen (I imagine it is somewhere far away due to a problem with the constraints?).
There really isn't much code for me to show. I literally just placed an UITableView into the interface builder, and followed that tutorial to set the delegate to my view controller. The methods would be
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
Where tableData is an array with 16 strings.
Try to remove all constraints, then select your tableView and add their again and press Add 4 constraints. Be sure, you don't have some ambiguity.
Related
This is the original code. I am a beginner in iOS development, how should I modify the code such that Admob ads will be loaded at the bottom of tableview? I tried to follow the tutorial from http://jmsliu.com/1207/add-google-admob-in-ios-apps.html , but can't get it to work.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.prefixArray count];
}
- (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];
}
NSDictionary *d = self.prefixArray[indexPath.row];
cell.textLabel.text = d[kMDTitleKey];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSDictionary *d = self.prefixArray[indexPath.row];
NSInteger entryID = [d[kMDIdentifierKey] integerValue];
self.searchBar.text = d[kMDTitleKey];
[self.db fetchDefinitionsWithID:entryID callback:^(NSDictionary *response) {
NSString *HTML = [self.HTMLRenderer renderHTML:response];
[self.webView loadHTMLString:HTML baseURL:nil];
[self.searchDisplayController setActive:NO animated:YES];
}];
}
The tutorial you share uses the UITableView's title header to show an ad. Probably that's not a good idea since you'll probably would need to use those headers for your own purposes.
I understand you want to put ads at the bottom of the screen. In that case it would be a good idea to use an UIView below UITableView. In that case you'll need to change your UITableViewController for an UIViewController to be able to do that.
If you want to add the add at the bottom of the UITableView you should think of it as the cell N+1 (being N the size of self.prefixArray) and update your table callbacks to handle the special case correctly.
You should add the advert view as the footerview for the tableview. Implement
(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
where you will create the advert view and return it as the footer view
dont put the advert view inside a cell, as it will be dequeued and its just the incorrect place for it.
I feed a UITableView with a list of names and images from a JSON. "SDWebImage" handles images download. It works OK apart from the fact that the images move to the left when the user selects a row or when scrolls the table view.
Two screen captures to show the issue.
Interface Builder setup
Implementation is pretty standard:
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil ) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"Cell"];
}
cell.textLabel.text = [[array objectAtIndex:indexPath.row]objectForKey:#"marca"];
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
[cell.imageView setImageWithURL:[NSURL URLWithString:[[array objectAtIndex:indexPath.row]objectForKey:#"photoUrl"]]placeholderImage:[UIImage imageNamed:#"placeholder.png"] ];
return cell;
}
What can I do to stop images moving?
I've just had a similar issue on an app and found it was because I was re-using UITableViewCells default imageView IBOutlet.
After creating my a new IBOutlet, called something other than imageView, and hooking it up it resolved the issue.
I'm working on my first app and I'm kind of new to Objective-C, I want to make it so when someone types in a text field then presses a button it will save it to a table view. Does anyone know how to do this or know any tutorials.
all you have to do is everytime you press the button you'll refresh/update your tableview.
- (IBAction)buttonPressed:(id)sender {
NSString *textNameToAdd = yourTextField.text;
[containerArrayForTableView addObject:textNameToAdd];
[myTableView reloadData];
}
// UITABLEVIEW DELEGATES
- (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.
// Usually the number of items in your array (the one that holds your list)
return [containerArrayForTableView count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//Where we configure the cell in each row
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell... setting the text of our cell's label
cell.textLabel.text = [containerArrayForTableView objectAtIndex:indexPath.row];
return cell;
}
refer here if you are having trouble configuring with UITableView.
These are some good tutorials for you..
http://www.appcoda.com/ios-programming-tutorial-create-a-simple-table-view-app/
http://www.codigator.com/tutorials/ios-uitableview-tutorial-for-beginners-part-1/
http://iosmadesimple.blogspot.in/2012/10/adding-uitableview-tutorial.html
Happy coding.
EDIT:
Here what i made for you:
Simple Demo With TextField & TableView
EDIT 2:
Simple Demo With 2 TextFields & 2 TableViews
MY UITableview Group style only showing one value from the array of 4 elements.
in viewdidload i add following array
_displayThemeItems=[[NSArray alloc]initWithObjects:#"Default",#"Night",#"Sepia" ,#"Blue" ,nil];
_displayThemeIcons=[[NSArray alloc]initWithObjects:[UIImage imageNamed:#"day.png"],[UIImage imageNamed:#"night.png"],[UIImage imageNamed:#"sepia.png"], [UIImage imageNamed:#"blue.png"],nil];
and my table delegate methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_displayThemeItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=#"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// No cell available - create one.
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];}
NSLog(#"rray%#",_displayThemeItems);
cell.textLabel.text = [_displayThemeItems objectAtIndex:indexPath.row];
cell.imageView.image=[_displayThemeIcons objectAtIndex:indexPath.row];
return cell;
}
Your code look fine to me, the only the only reason that I can think amy effect the result you see is how you set up your table view.
Make sure that your table view frame is bug enough to show the 4 items, and make sure you set the tabe view data source.
Add the "}" just before the return such that the code where you set the title and image comes under the "if" statement.
I've noticed the cells are taken out of memory when scrolled out of view and then added back into memory when scrolled into view.
I get the performance reasons for this, but since my table only has a couple of cells out of view, is there a way to turn off this caching feature?
You could try using a different cell identifier for each index path.
That way, the UITableView won't be able to find a cell to dequeue, and a new one will be provided.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString * CellIdentifier = [NSString stringWithFormat:#"%d - %d", indexPath.row, indexPath.section];
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
return cell;
}
You could also skip queuing/dequeing entirely, and retain a reference to them yourself, then just return them in the cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
return [myCells objectAtIndex:indexPath.row];
}
Where myCells is an array holding your UITableViewCells.