![](/img/trans.png)
[英]in UITableView, can I have two sections with the cells of each section to be variable length?
[英]How can i start my UITableView section with number 0 when i have multiple sections?
=> 我知道,这是一个愚蠢的问题,但这对像我这样的新iPhone开发者来说更为重要。
在这里我有与UITableView部分有关的问题:
第一个问题:我有一个疑问,如何从多个部分中以数字0开始我的UITableView部分?
例如 :
我在UITableView
和numberOfSectionsInTableView
委托方法中有11个部分,我的部分首先是从第11个部分开始,然后是0、1、2、3、4、5、6、7、8、9和10。我从0号开始我的段号,然后按原样。
第二个问题:在titleForHeaderInSection
委托方法中,tableView的所有部分都重复3次。 在当前ViewController上,我向上或向下滚动UITableView,然后该部分显示为正常(0到11),重复了3次后,该UITableView的正常部分显示出来。
例如 :
我使用NSLog (@" %d ", section );
在titleForHeaderInSection
委托方法中,然后显示在控制台中,如下所示
控制台输出:
2012-09-17 12:27:47.424 Quotes2You[1623:f803] 11
2012-09-17 12:27:47.426 Quotes2You[1623:f803] 11
2012-09-17 12:27:47.427 Quotes2You[1623:f803] 11
2012-09-17 12:27:47.428 Quotes2You[1623:f803] 0
2012-09-17 12:27:47.429 Quotes2You[1623:f803] 0
2012-09-17 12:27:47.429 Quotes2You[1623:f803] 0
2012-09-17 12:27:47.430 Quotes2You[1623:f803] 1
2012-09-17 12:27:47.431 Quotes2You[1623:f803] 1
2012-09-17 12:27:47.431 Quotes2You[1623:f803] 1
2012-09-17 12:27:47.432 Quotes2You[1623:f803] 2
2012-09-17 12:27:47.433 Quotes2You[1623:f803] 2
2012-09-17 12:27:47.433 Quotes2You[1623:f803] 2
2012-09-17 12:27:47.434 Quotes2You[1623:f803] 3
2012-09-17 12:27:47.435 Quotes2You[1623:f803] 3
2012-09-17 12:27:47.436 Quotes2You[1623:f803] 3
2012-09-17 12:27:47.436 Quotes2You[1623:f803] 4
2012-09-17 12:27:47.437 Quotes2You[1623:f803] 4
2012-09-17 12:27:47.438 Quotes2You[1623:f803] 4
2012-09-17 12:27:47.438 Quotes2You[1623:f803] 5
2012-09-17 12:27:47.439 Quotes2You[1623:f803] 5
2012-09-17 12:27:47.439 Quotes2You[1623:f803] 5
2012-09-17 12:27:47.440 Quotes2You[1623:f803] 6
2012-09-17 12:27:47.441 Quotes2You[1623:f803] 6
2012-09-17 12:27:47.462 Quotes2You[1623:f803] 6
2012-09-17 12:27:47.463 Quotes2You[1623:f803] 7
2012-09-17 12:27:47.464 Quotes2You[1623:f803] 7
2012-09-17 12:27:47.465 Quotes2You[1623:f803] 7
2012-09-17 12:27:47.466 Quotes2You[1623:f803] 8
2012-09-17 12:27:47.466 Quotes2You[1623:f803] 8
2012-09-17 12:27:47.467 Quotes2You[1623:f803] 8
2012-09-17 12:27:47.472 Quotes2You[1623:f803] 9
2012-09-17 12:27:47.476 Quotes2You[1623:f803] 9
2012-09-17 12:27:47.478 Quotes2You[1623:f803] 9
2012-09-17 12:27:47.480 Quotes2You[1623:f803] 10
2012-09-17 12:27:47.481 Quotes2You[1623:f803] 10
2012-09-17 12:27:47.481 Quotes2You[1623:f803] 10
2012-09-17 12:27:47.487 Quotes2You[1623:f803] 0
2012-09-17 12:27:47.487 Quotes2You[1623:f803] 1
2012-09-17 12:27:47.489 Quotes2You[1623:f803] 2
首先,这重复所有部分3次,然后当我滚动UITableView然后部分从0开始(正常)
我的代码在这里:
ThirdViewController.h
@interface ThirdViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
{
UITableView *tblCustomer;
NSArray *listOfsection;
}
@property (nonatomic,retain) UITableView *tblCustomer;
@property (nonatomic,retain) NSArray *listOfsection;
@end
ThirdViewController.m
@synthesize tblCustomer , listOfsection;
- (void)viewDidLoad
{
[super viewDidLoad];
self.tblCustomer = [[UITableView alloc] initWithFrame:CGRectMake(0,0,320,417) style:UITableViewStyleGrouped];
self.tblCustomer.delegate = self;
self.tblCustomer.dataSource = self;
[self.view addSubview:self.tblCustomer];
self.listOfsection = [[NSArray alloc] initWithObjects:@"Name", @"Company", @"Address", @"Email", @"Mobile", @"Phone", @"Potential", @"Sales Status", @"Genre", @"Distributor", @"Dist Rep", @"Internal Notes", nil];
}
#pragma mark - UITableView Datasource Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView {
return [self.listOfsection count];
}
- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section
{
int numSection=0;
if (section == 2)
numSection = 5;
else
numSection = 1;
return numSection;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// here is my code cellForRowAtIndexPath as section wise
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSLog(@" %d ", section);
NSString *sectionHeader = nil;
sectionHeader = [self.listOfsection objectAtIndex:section];
// here is section overwrite //
return sectionHeader;
}
#pragma mark - Memory Management
-(void)dealloc
{
[super dealloc];
[self.listOfsection release];
[self.tblCustomer release];
}
提前致谢
你问:
第一个问题:我有一个疑问,如何从多个部分中以数字0开始我的UITableView部分?
我认为您之所以惊慌,是因为这些操作并非严格按顺序进行。 这就是它发生的方式,您无法更改此行为。 显然,您的部分在应用程序中以正确的顺序显示,但是它们恰好不会被顺序调用。 这只是事件驱动的编程的本质。 但是,我发现您的代码中没有任何东西会因执行顺序而中断,但是也许您的代码中没有包含某些需要严格顺序调用这些方法的内容,如果是这样,您将必须重构该代码。 但是我在上面的代码片段中什么也没看到。 UITableViewDataSource
委托方法的性质意味着您可以(并且应该)以不依赖于调用单元格或节的顺序的方式来编写方法。 永远不要依靠UI中的事件顺序来控制如何构建视图的基础模型。
第二个问题:在titleForHeaderInSection委托方法中,tableView的所有部分都重复3次。 在当前ViewController上,我向上或向下滚动UITableView,然后该部分显示为正常(0到11),重复了3次后,该UITableView的正常部分显示出来。
如果连续被调用三遍,我会感到惊讶。 我敢打赌(特别是鉴于您的日志显示了格式稍有不同的NSLog
语句的证据),您的代码中还有其他NSLog
语句。 我会尝试将您的NSLog
语句更改为类似以下内容:
NSLog(@"%s section=%d", __FUNCTION__, section);
这样,(a)您知道从哪个方法记录日志; (b)通过在格式字符串中添加描述性的"section="
,这意味着如果您永远不会对正在记录的数字感到困惑。 我愿意打赌,这三个NSLog
语句并非全部来自您的titleForHeaderInSection
。 我个人不再将__FUNCTION__
引用放在NSLog
语句中,因为很容易混淆生成NSLog
语句的内容。
但是,即使titleForHeaderInSection
调用titleForHeaderInSection
,那又如何呢? iOS已经在幕后进行了优化,可以处理各种事情。 了解我们正在发生的事情对我们很有好处(例如,确保您不会像titleForHeaderInSection
计算密集型活动或网络密集型活动那样愚蠢地做这些事情),但这是拥抱平静祈祷并感谢我们作为开发人员可以控制的时候的时候结束,而我们没有。 (并且UITableViewDataSource
调用的顺序和数量是我们无法控制的事情之一。)只要您不是方法的多余调用的源(例如,执行不必要的reloadData
调用),我就不必担心关于它。
综上所述,虽然titleForHeaderInSection
连续被调用了3次会令我感到惊讶,但不要依赖于它仅被调用一次。 我认为表格视图可能会针对表格的每个部分调用一次(可能要进行某种操作,例如找出整个表格的高度,以便滚动条的大小得到适当调整),然后针对屏幕上可见的部分再次调用它(以实际显示节标题)。
-(NSInteger)numberOfRowsInTotal
{
NSInteger sections = self.numberOfSections;
NSInteger cellCount = 0;
for (NSInteger i = 0; i < sections; i++)
{
cellCount += [self numberOfRowsInSection:i];
}
return cellCount;
}
这种方式不合适,但是您可以使用下面的代码及其完美的工作方式。
[self reloadDataWithCompletion:^{
communityFoundIndex = [[NSMutableArray alloc]init];
NSRange range = NSMakeRange(0, 1);
NSIndexSet *section = [NSIndexSet indexSetWithIndexesInRange:range];
[self.myTbl reloadSections:section withRowAnimation:UITableViewRowAnimationNone];
}];
- (void) reloadDataWithCompletion:( void (^) (void) )completionBlock {
[_tblCommunity reloadData];
if(completionBlock) {
completionBlock();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.