I followed the tutorial here and was wondering how to make the table appear grouped.
ex:
group1 contains Subview One and Subview Two
group2 contains Subview Three
I switched the type in interface builder but that only shows one group.
Thanks,
Adam
Sidenote* I am a completely new at objective c, hence the tutorial.
EDIT
I thought it might be helpful to put up the piece of code
#import "RootViewController.h"
#import "SubViewOneController.h"
#import "SubViewTwoController.h"
#implementation RootViewController
#pragma mark -
#pragma mark View lifecycle
-(void)awakeFromNib {
views = [[NSMutableArray alloc] init];
SubViewOneController *subViewOneController = [[SubViewOneController alloc] init];
SubViewTwoController *subViewTwoController = [[SubViewTwoController alloc] init];
//Subview 1
subViewOneController.title = #"Subview One";
[views addObject:[NSDictionary dictionaryWithObjectsAndKeys:
#"Subview One", #"title",
subViewOneController, #"controller",
nil]];
[subViewOneController release];
//Subview 2
subViewOneController = [[SubViewOneController alloc] init];
subViewOneController.title = #"Subview Two";
[views addObject:[NSDictionary dictionaryWithObjectsAndKeys:
#"Subview Two", #"title",
subViewTwoController, #"controller",
nil]];
[subViewOneController release];
//Subview 3
subViewOneController = [[SubViewOneController alloc] init];
subViewOneController.title = #"Subview Three";
[views addObject:[NSDictionary dictionaryWithObjectsAndKeys:
#"Subview Three", #"title",
subViewOneController, #"controller",
nil]];
[subViewOneController release];
UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
temporaryBarButtonItem.title = #"Back";
self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
[temporaryBarButtonItem release];
self.title = #"Basic Navigation";
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [views count];
}
//
//I think it goes somewhere in here
//
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewStyleGrouped reuseIdentifier:CellIdentifier];
}
cell.text = [[views objectAtIndex:indexPath.row] objectForKey:#"title"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIViewController *targetViewController = [[views objectAtIndex:indexPath.row] objectForKey:#"controller"];
[[self navigationController] pushViewController:targetViewController animated:YES];
}
- (void)dealloc {
[views dealloc];
[super dealloc];
}
#end
Ok so you are correct it does go into your cellForRowAtIndexPath: you will be looking to do something like:
if ([indexPath section] == 0) {
//stuff for first section.
}
if ([indexPath section] == 1) {
//stuff for second section.
}
You will need to also deal with how your are configuring the cell from your array. The sections row numbers start with 0. The same thing with didSelectRowAtIndexPath:
If you don't deal with the section where you set the cell.text you will end up with
Subview One
Subview Two
Subview One
I recommend getting the iPhone Programming (The Big Nerd Ranch Guide)
In Interface Builder, choose your UITableView object and choose Grouped as the style.
If you are programmatically creating it, use the initWithStyle: UITableViewStyleGrouped.
EDIT:
if (section == 0) { /* your first section */ }
if (section == 1) { /* your second section */ }
This is all controlled by your UITableViewDataSource. The numberOfSectionsInTableView: method controls how many groups there are, and tableView:numberOfRowsInSection: controls how many rows are in each group.
Your tableView:cellForRowAtIndexPath: on the UITableViewDelegate gets an NSIndexPath object; indexPath.section tells you which group and indexPath.row tells you which row in the group. The cell you return really has no idea which group it is in or which row in the group it is, it's all controlled by the fact that you return it for a particular indexPath when tableView:cellForRowAtIndexPath: is called.
Related
I have a table view. Now I want to collapse and expand rows by tapping on the section header. In other words, when I tap the header the rows display for that section. How can I do this?
I draft up some code to give you the idea. The concept is we keep track of collapsed section in NSMutableSet and add/remove it according to the user touch on the section. The collapse/expand animation is actually the animation of adding/removing cells.
#import "ViewController.h"
#interface ViewController () < UITableViewDataSource, UITableViewDelegate> {
NSMutableSet* _collapsedSections;
}
#property (nonatomic, weak) IBOutlet UITableView* tableView;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_collapsedSections = [NSMutableSet new];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_collapsedSections containsObject:#(section)] ? 0 : 10;
}
-(NSArray*) indexPathsForSection:(int)section withNumberOfRows:(int)numberOfRows {
NSMutableArray* indexPaths = [NSMutableArray new];
for (int i = 0; i < numberOfRows; i++) {
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:i inSection:section];
[indexPaths addObject:indexPath];
}
return indexPaths;
}
-(void)sectionButtonTouchUpInside:(UIButton*)sender {
sender.backgroundColor = [UIColor greenColor];
[self.tableView beginUpdates];
int section = sender.tag;
bool shouldCollapse = ![_collapsedSections containsObject:#(section)];
if (shouldCollapse) {
int numOfRows = [self.tableView numberOfRowsInSection:section];
NSArray* indexPaths = [self indexPathsForSection:section withNumberOfRows:numOfRows];
[self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
[_collapsedSections addObject:#(section)];
}
else {
int numOfRows = 10;
NSArray* indexPaths = [self indexPathsForSection:section withNumberOfRows:numOfRows];
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
[_collapsedSections removeObject:#(section)];
}
[self.tableView endUpdates];
//[_tableView reloadData];
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIButton* result = [UIButton buttonWithType:UIButtonTypeCustom];
[result addTarget:self action:#selector(sectionButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
result.backgroundColor = [UIColor blueColor];
[result setTitle:[NSString stringWithFormat:#"Section %d", section] forState:UIControlStateNormal];
result.tag = section;
return result;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
result.textLabel.text = [NSString stringWithFormat:#"Cell %d", indexPath.row];
return result;
}
#end
As already this is a old question now.Me too also searched and in the last got 1 sample code from Github.So I thought of sharing the link,if in near future any one comes up with the same issues.
https://github.com/iSofTom/STCollapseTableView
TLIndexPathTools does this with only a few lines of code on your part. Try running the Collapse sample project. It subclasses TLCollapsibleTableViewController, which has a couple of nice options. It supports expanding a single section at a time or multiple sections. It also optimizes the scroll position when you expand a section to show as many rows of the section as possible. So if you tap on a section near the bottom of the screen, it will scroll up automatically.
The full view controller code of the sample project is as follows:
#import "TLCollapsibleTableViewController.h"
#interface CollapseTableViewController : TLCollapsibleTableViewController
- (IBAction)toggleSingleSectionExpanded:(UISwitch *)sender;
#end
#import "CollapseTableViewController.h"
#import "TLIndexPathSectionInfo.h"
#import "TLCollapsibleDataModel.h"
#define SECTION1_NAME #"Section 1"
#define SECTION2_NAME #"Section 2"
#interface CollapseTableViewController ()
#property (strong, nonatomic) TLIndexPathDataModel *backingDataModel;
#end
#implementation CollapseTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//define items for two sections
NSArray *section1Items = #[
#"Fredricksburg",
#"George Washington",
#"Grand Canyon"];
NSArray *section2Items = #[
#"Jelly Bean",
#"Bibliography",
#"Keyboard Shortcut",
#"Metadata",
#"Fundamental",
#"Cellar Door"];
//We're using plain string items, so we don't have a sectionNameKeyPath property
//to use, so instead we explicitly create section info objects
TLIndexPathSectionInfo *section1 = [[TLIndexPathSectionInfo alloc] initWithItems:section1Items andName:SECTION1_NAME];
TLIndexPathSectionInfo *section2 = [[TLIndexPathSectionInfo alloc] initWithItems:section2Items andName:SECTION2_NAME];
//create the backing model, which contains all sections and items
self.backingDataModel = [[TLIndexPathDataModel alloc] initWithSectionInfos:#[section1, section2]
andIdentifierKeyPath:nil andCellIdentifierKeyPath:nil];
[self collapseAll];
}
- (void)tableView:(UITableView *)tableView configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
NSString *item = [self.dataModel itemAtIndexPath:indexPath];
cell.textLabel.text = item;
}
- (IBAction)toggleSingleSectionExpanded:(UISwitch *)sender {
self.singleExpandedSection = sender.isOn;
[self collapseAll];
}
- (void)collapseAll
{
self.dataModel = [[TLCollapsibleDataModel alloc] initWithBackingDataModel:self.backingDataModel
collapsedSectionNames:[NSSet setWithArray:self.backingDataModel.sectionNames]];
}
#end
configure viewForHeaderInSection:
like this
(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UILabel *lblHeader = [[UILabel alloc]init];
lblHeader.text = #"Section 0";
lblHeader.backgroundColor = [UIColor blueColor];
lblHeader.font = [UIFont fontWithName:#"Avenir" size:18];
lblHeader.textAlignment=NSTextAlignmentLeft;
lblHeader.userInteractionEnabled=YES;
UIGestureRecognizer *gr;
if(section==0){
lblHeader.text = #"Section 0";
gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
}else if(section == 1){
lblHeader.text = #"Section 1";
gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture1:)];
}
[lblHeader addGestureRecognizer:gr];
return lblHeader;
}
then write seperate action calls
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer {
}
- (void)handleGesture1:(UIGestureRecognizer *)gestureRecognizer {
}
I currently have developed a Tabbed Based Application. The first Tab is decors which displays Colour Swatches or Images in a TableView Structure. Currently when you push on a image or Colour swatch An alert pops up saying which table cell you have pushed. I instead want to link each table cell image or Colour swatch to a new view controller showing a bigger image of that image or colour swatch. A modal would also do fine
#import "TableViewsViewController.h"
#implementation TableViewsViewController
#pragma mark -
#pragma mark Synthesizers
#synthesize table;
#synthesize sitesArray;
#synthesize imagesArray;
#pragma mark -
#pragma mark View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
// Load up the sitesArray with a dummy array : sites
NSArray *sites = [[NSArray alloc] initWithObjects:#"a", #"b", #"c", #"d", #"e", #"f", #"g", #"h", nil];
self.sitesArray = sites;
[sites release];
UIImage *active = [UIImage imageNamed:#"a.png"];
UIImage *ae = [UIImage imageNamed:#"b.png"];
UIImage *audio = [UIImage imageNamed:#"c.png"];
UIImage *mobile = [UIImage imageNamed:#"d.png"];
UIImage *net = [UIImage imageNamed:#"e.png"];
UIImage *photo = [UIImage imageNamed:#"f.png"];
UIImage *psd = [UIImage imageNamed:#"g.png"];
UIImage *vector = [UIImage imageNamed:#"h.png"];
NSArray *images = [[NSArray alloc] initWithObjects: active, ae, audio, mobile, net, photo, psd, vector, nil];
self.imagesArray = images;
[images release];
[super viewDidLoad];
}
#pragma mark -
#pragma mark Table View datasource methods
// Required Methods
// Return the number of rows in a section
-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
return [sitesArray count];
}
// Returns cell to render for each row
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Configure cell
NSUInteger row = [indexPath row];
// Sets the text for the cell
//cell.textLabel.text = [sitesArray objectAtIndex:row];
// Sets the imageview for the cell
cell.imageView.image = [imagesArray objectAtIndex:row];
// Sets the accessory for the cell
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// Sets the detailtext for the cell (subtitle)
//cell.detailTextLabel.text = [NSString stringWithFormat:#"This is row: %i", row + 1];
return cell;
}
// Optional
// Returns the number of section in a table view
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
#pragma mark -
#pragma mark Table View delegate methods
// Return the height for each cell
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 78;
}
// Sets the title for header in the tableview
-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return #"Decors";
}
// Sets the title for footer
-(NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return #"Decors";
}
// Sets the indentation for rows
-(NSInteger) tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 0;
}
// This method is run when the user taps the row in the tableview
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Tapped row!"
message:[NSString stringWithFormat:#"You tapped: %#", [sitesArray objectAtIndex:indexPath.row]]
delegate:nil
cancelButtonTitle:#"Yes, I did!"
otherButtonTitles:nil];
[alert show];
[alert release];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
NSLog(#"Memory Warning!");
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
self.table = nil;
self.sitesArray = nil;
self.imagesArray = nil;
[super viewDidUnload];
}
- (void)dealloc {
[table release];
[sitesArray release];
[imagesArray release];
[super dealloc];
}
#end
Part where the Alert is
// This method is run when the user taps the row in the tableview
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Tapped row!"
message:[NSString stringWithFormat:#"You tapped: %#", [sitesArray objectAtIndex:indexPath.row]]
delegate:nil
cancelButtonTitle:#"Yes, I did!"
otherButtonTitles:nil];
[alert show];
[alert release];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
In didSelectRowAtIndexPath you can just init another view controller and present. You can present it from self.navigationController so that there is a back button if you wish. Here I show it presented modally:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Deselect row
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// Declare the view controller
UIViewController *anotherVC = nil;
// Determine the row/section on the tapped cell
switch (indexPath.section) {
case 0:
switch (indexPath.row) {
case 0: {
// initialize and allocate a specific view controller for section 0 row 0
anotherVC = [[ViewControllerForRowZeroSectionZero alloc] init];
break;
}
case 1: {
// initialize and allocate a specific view controller for section 0 row 1
anotherVC = [[ViewControllerForRowOneSectionZero alloc] init];
break;
}
}
break;
case 1: {
// initialize and allocate a specific view controller for section 1 ALL rows
anotherVC = [[ViewControllerForAllRowsSectionOne alloc] init];
break;
}
}
// Get cell textLabel string to use in new view controller title
NSString *cellTitleText = [[[tableView cellForRowAtIndexPath:indexPath] textLabel] text];
// Get object at the tapped cell index from table data source array to display in title
id tappedObj = [sitesArray objectAtIndex:indexPath.row];
// Set title indicating what row/section was tapped
[anotherVC setTitle:[NSString stringWithFormat:#"You tapped section: %d - row: %d - Cell Text: %# - Sites: %#", indexPath.section, indexPath.row, cellTitleText, tappedObj]];
// present it modally (not necessary, but sometimes looks better then pushing it onto the stack - depending on your App)
[anotherVC setModalPresentationStyle:UIModalPresentationFormSheet];
// Have the transition do a horizontal flip - my personal fav
[anotherVC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
// The method `presentModalViewController:animated:` is depreciated in iOS 6 so use `presentViewController:animated:completion:` instead.
[self.navigationController presentViewController:anotherVC animated:YES completion:NULL];
// We are done with the view controller. It is retained by self.navigationController so we can release it (if not using ARC)
[anotherVC release], anotherVC = nil;
}
You're 90% of the way there! In didSelectRowAtIndexPath, instead of putting up the alert, instantiate and set up the view controller whose view you want to show and call presentViewController:animated:.
See my book: http://www.apeth.com/iOSBook/ch19.html#_presented_view_controller
Are u using Storyboard?
If so, you can build a segue, set it to Modal or Push. Pointing from the TableView to the destination ViewController
Then name it with an identifier in the segue's inspector.
Then in didSelectRowAtIndexPath, call performSegue: withIdentifier
You can also setup the destination ViewController based on the cell selected in prepareForSegue.
Hope it helps.
I use predicate to filter a table.
It works very well and also to display the detailView for each cell.
But after a filtering it does not work to display the filtered list in the detailView.
The cell just turns blue when pressing it but does not shift to detailView.
What am I missing here?
The code looks like this.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
Person *person;
if (tableView == self.searchDisplayController.searchResultsTableView)
{
person = [self.filteredPeople objectAtIndex:indexPath.row];
}
else
{
person = [self.people objectAtIndex:indexPath.row];
}
dvController.title = person.firstName;
dvController.price = person.sjuName;
dvController.link = person.sexName;
dvController.picture = [UIImage imageNamed:person.femName];
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
dvController = nil;
}
Here both the tableView are of different class so the can't be equal thus change your if condition as
if ([tableView class] != [UITableView class]) // if it's is of type UISearchResultsTableView
{
person = [self.filteredPeople objectAtIndex:indexPath.row];
}
else
{
person = [self.people objectAtIndex:indexPath.row];
}
In the following code, I push a new UITableView when I click on a button. The TableView has 25 rows. I can see the first 9 row and the 10th row got cut off. I can't scroll down to view the rest of the rows. How do I make the TableView scrollable? Thank you.
- (IBAction)showListPicker:(id)sender {
ListPicker *lp = [[ListPicker alloc] initWithStyle:UITableViewStyleGrouped];
[self setTitle:#"Home"];
[[self navigationController] pushViewController:lp animated:YES];
[lp release];
}
The following is the declaration for ListPicker
#import <Foundation/Foundation.h>
#interface ListPicker : UITableViewController
{}
#end
The following is the implementation for ListPIcker
#import "ListPicker.h"
#import "GameStore.h"
#import "List.h"
#import "HomeViewController.h"
#implementation ListPicker
- (id)init
{
return [super initWithStyle:UITableViewStyleGrouped];
}
-(id)initWithStyle:(UITableViewStyle)style{
return [self init];
}
-(void)dealloc{
[super dealloc];
}
-(void)viewDidLoad{
[self setTitle:#"Select List"];
[[self navigationController] setNavigationBarHidden:NO];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [[[GameStore defaultStore] allLists] count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"UITableViewCell"];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"UITableViewCell"] autorelease];
}
NSArray *allList = [[GameStore defaultStore] allLists];
List *mylist = [allList objectAtIndex:[indexPath row]];
NSString *listTitle = [mylist label];
[[cell textLabel] setText:listTitle];
[cell setAccessoryType:UITableViewCellAccessoryNone];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
List *list = [[[GameStore defaultStore] allLists] objectAtIndex:[indexPath row]];
[[GameStore defaultStore] setSelectedList:list];
HomeViewController *hv = [[HomeViewController alloc] init];
[[self navigationController] popViewControllerAnimated:YES];
}
#end
Try to add:
yourTable.scrollEnabled = YES;
Also, when you create your table programmatically, make sure it is "long enough", i.e.,
CGRect cgRct = CGRectMake(0, 0, 320, 600);
yourTable = [[UITableView alloc] initWithFrame:cgRct style:UITableViewStyleGrouped];
Instead of YES, write true as it accepts only bool values.
yourTable.scrollEnabled = true;
Two common ways to change the boolean state of scrolling in a UITableView:
1) It can be enabled either in the attributes inspector of the UITableView via Storyboard.
2) Or programmatically inside your viewDidLoad() via ViewController.
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet var yourTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// assign delegate and datasource
yourTable.delegate = self
yourTable.dataSource = self
//enable UITableView scrolling in Swift 3 and Swift 4.
yourTable.isScrollEnabled = true
}
}
below test code causes EXC_BAD_ACCESS when it's scrolled many time.
would somebody like to tell me the problem?
-myview.h---------------------------------------------------
#interface MyView : UINavigationController < UITableViewDataSource, UITableViewDelegate, UINavigationBarDelegate >
{
UITableView* mTableView;
NSMutableArray* data;
}
-myview.m---------------------------------------------------
#implementation MyView
- (void)viewDidLoad
{
[super viewDidLoad];
int th = self.navigationBar.frame.size.height;
int w = self.view.frame.size.width;
int h = self.view.frame.size.height;
mTableView = [[[UITableView alloc] initWithFrame:
CGRectMake(0.0f, th, w, h - th) style:UITableViewStylePlain] retain];
mTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight;
mTableView.dataSource = self;
mTableView.delegate = self;
[mTableView setRowHeight:55];
[self.view addSubview:mTableView];
data = [[[NSMutableArray alloc] init] retain];
for (int i = 0; i < 150; i++)
{
[data addObject:[NSDictionary
dictionaryWithObjects:[NSArray arrayWithObjects:#"good", [UIColor blackColor], nil]
forKeys:[NSArray arrayWithObjects:#"name", #"color", nil]]];
}
}
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 150;
}
- (UITableViewCell *)tableViewCellWithReuseIdentifier:(NSString *)identifier
{
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
cell.textLabel.text = #"goooood";
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [self tableViewCellWithReuseIdentifier:CellIdentifier];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (void)dealloc
{
[super dealloc];
}
- (BOOL)canBecomeFirstResponder
{
return YES;
}
#end
You should have a view controller that contains your table view (it could even be a subclass of UITableViewController). I don't believe that UINavigationController is mean't to be subclassed as you have. If you want a navigation controller you should create an instance of CustomTableViewController, containing your tableview, and then use the initWithRootViewController: method of UINavigationController:
CustomTableViewController * ctvc = [[CustomTableViewController alloc] init];
UINavigationController * navigationController = [[UINavigationController alloc] initWithRootViewController:ctvc];
[ctvc release];
// Superview is where you want this added
// probably your window in app did finish launching
[superview addSubview:navigationController.view];
[navigationController release];