網頁

2012年4月2日 星期一

iOS5 TableViewController

.h
  
@interface TabTable : UITableViewController
{

    NSMutableArray *yourData;

}

@end
.m
1.
  
- (void)viewDidLoad

{

    [super viewDidLoad];

   

    yourData = [NSMutableArray arrayWithObjects:@"xxx",@"xxx",nil];

}
2.
  
- (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 [yourData count];

}
3.
  
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

  // Configure the cell...

    cell.textLabel.text = [yourData objectAtIndex:indexPath.row];

   

    return cell;

} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellLocation"; //Identifier一定要一樣才能推到下一頁
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    // Configure the cell...
    cell.textLabel.text = [yourData objectAtIndex:indexPath.row];
    
    return cell;
}
與Table Cell Data與json結合 {"locations":["Taipei","Taiwan","SomeWhere"]}
  
- (void)viewDidLoad
{
    [super viewDidLoad];
    locationDataURL = [NSURL URLWithString:@"http://localhost/TabLocation.php"];
    NSString *jsonReturn = [[NSString alloc] initWithContentsOfURL:locationDataURL encoding:NSUTF8StringEncoding error:nil];
    
    SBJsonParser *parser = [[SBJsonParser alloc] init];
    
    // json-->NSDictionary
    NSDictionary *jSON = [parser objectWithString:jsonReturn error:nil];  
    
    locationData =  [jSON objectForKey:@"locations"]; 
    NSLog(@"jsonFindLocations %@",locationData);
}

StoryTable[4905:207] jsonFindLocations ( Taipei, Taiwan, SomeWhere )


 select版本 注意有部分與row剛好反過來
.h
  
#import 

@interface Information : UITableViewController
{
        NSArray *InfoData;
}

@end
.m
  
- (void)viewDidLoad
{
    [super viewDidLoad];

     InfoData = [[NSArray alloc] initWithObjects: @"About Us", @"App Store", @"Others", nil];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return [InfoData count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cellinfo";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    // Configure the cell...
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    cell.textLabel.text = [InfoData objectAtIndex:indexPath.section];
    cell.textLabel.font = [UIFont boldSystemFontOfSize: 18.0f];
 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    
    return cell;
}

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。