iPhone TableView数据转换成2个部分 [英] iPhone TableView Data Into 2 Sections
问题描述
我很喜欢这个,但下面是我的代码snippet
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section == 0)
{
contentArray = [[NSArray arrayWithObjects:@Send SMS,@Reports,nil] retain];
}
if(section == 1)
{
contentArray = [[NSArray arrayWithObjects:@Accounts,nil] retain];
}
return [contentArray count];
}
我已将数据成功分割为两个部分,但填充行
可以任何一个帮助...
首先,你在你提供的代码中有一个漏洞。删除 retain
的两个调用。
其次,您在使用多个开关/ if的经典问题。基于相同的信息链接。
首先创建一个TableSection类:
@interface TableSection:NSObject
{}
@property(nonatomic,copy)NSString * header;
@property(nonatomic,copy)NSArray * rows;
- (NSInteger)numberOfRows;
- (UITableViewCell *)cellInTableView:(UITableView *)tableView forRow:(NSInteger)row;
@end
@implementation TableSection
@synthesize header;
@synthesize rows;
- (void)dealloc {
[header release];
[rows release];
[super dealloc]
}
- (NSInteger)numberOfRows {
return rows.count;
}
- (UITableViewCell *)cellInTableView:(UITableView *)tableView forRow:(NSInteger)row {
//创建/重用,设置并返回一个UITableViewCell
}
@end
现在在您的TableViewController
@interface MyViewController:UITableViewController
{}
@property(nonatomic,retain)NSArray * tableSections;
@end
@implementation MyViewController
- (void)dealloc {
[tableSections]
[super dealloc]
}
- (void)viewDidLoad {
TableSection * section1 = [[TableSection alloc] init];
[section1 setRows:[NSArray arrayWithObjects:@Send SMS,@Reports,nil]];
TableSectlion * section2 = [[TableSection alloc] init];
[section2 setRows:[NSArray arrayWithObjects:@Accounts,nil]];
[self setTableSections:[NSArray arrayWithObjects:section1,section2,nil]];
[section2 release];
[section1 release];
}
- (void)viewDidUnload {
[self setTableSections:nil];
}
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.tableSections.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[self.tableSections objectAtIndex:section] numberOfRows]
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
return [[self.tableSections objectAtIndex:indexPath .section] cellInTableView:tableView forRow:indexPath.row];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [[self.tableSections objectAtIndex:section] header];
}
@end
I am trying to get a tableview to split data i have in an array into sections...
I'm pretty new to this but below is my code snippet
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section == 0)
{
contentArray = [[NSArray arrayWithObjects:@"Send SMS", @"Reports", nil] retain];
}
if(section == 1)
{
contentArray = [[NSArray arrayWithObjects:@"Accounts", nil] retain];
}
return [contentArray count];
}
I have split the data successfully into the 2 sections but when populating the rows It just repeats the first content array in both sections.
Can any one help...
Thanks
First things first, you have a leak in the code you presented. Remove the two calls to retain
.
Second, you are in the classic problem of having multiple switches/if..else chains based on the same information. This screams for an OO solution.
First create a TableSection class:
@interface TableSection : NSObject
{ }
@property (nonatomic, copy) NSString* header;
@property (nonatomic, copy) NSArray* rows;
- (NSInteger)numberOfRows;
- (UITableViewCell*)cellInTableView: (UITableView*)tableView forRow: (NSInteger)row;
@end
@implementation TableSection
@synthesize header;
@synthesize rows;
- (void)dealloc {
[header release];
[rows release];
[super dealloc];
}
- (NSInteger)numberOfRows {
return rows.count;
}
- (UITableViewCell*)cellInTableView: (UITableView*)tableView forRow: (NSInteger)row {
// create/reuse, setup and return a UITableViewCell
}
@end
Now in your TableViewController
@interface MyViewController : UITableViewController
{ }
@property (nonatomic, retain) NSArray* tableSections;
@end
@implementation MyViewController
- (void)dealloc {
[tableSections release];
[super dealloc];
}
- (void)viewDidLoad {
TableSection* section1 = [[TableSection alloc] init];
[section1 setRows: [NSArray arrayWithObjects: @"Send SMS", @"Reports", nil]];
TableSectlion* section2 = [[TableSection alloc] init];
[section2 setRows: [NSArray arrayWithObjects: @"Accounts", nil]];
[self setTableSections: [NSArray arrayWithObjects: section1, section2, nil]];
[section2 release];
[section1 release];
}
- (void)viewDidUnload {
[self setTableSections: nil];
}
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView: (UITableView*)tableView {
return self.tableSections.count;
}
- (NSInteger)tableView: (UITableView*)tableView numberOfRowsInSection: (NSInteger)section {
return [[self.tableSections objectAtIndex: section] numberOfRows];
}
- (UITableViewCell*)tableView: (UITableView*)tableView cellForRowAtIndexPath: (NSIndexPath*)indexPath {
return [[self.tableSections objectAtIndex: indexPath.section] cellInTableView: tableView forRow: indexPath.row];
}
- (NSString*)tableView: (UITableView*)tableView titleForHeaderInSection: (NSInteger)section {
return [[self.tableSections objectAtIndex: section] header];
}
@end
这篇关于iPhone TableView数据转换成2个部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!