当我有多个部分时,如何以数字0开头UITableView部分? [英] How can i start my UITableView section with number 0 when i have multiple sections?

查看:42
本文介绍了当我有多个部分时,如何以数字0开头UITableView部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

=> 我知道,这是一个愚蠢的问题,但这对像我这样的新iPhone开发者来说更​​为重要.

在这里我有关于 UITableView部分的问题:

here i have question related to UITableView section :

第一个问题:我有一个问题,如何从多个部分中以数字0开始我的UITableView部分?

First Problem : i have question that how can i start my UITableView section with number 0 from multiple section ?

例如:

UITableView numberOfSectionsInTableView 委托方法中,我有11个节,我的节首先从第11个节开始,然后是0、1、2、3,4,5,6,7,8,9和10.所以我该如何从0号开始我的段号,然后按原样.

I have 11 number of section in UITableView and in numberOfSectionsInTableView delegate method, my section is first start with 11th number of section then 0,1,2,3,4,5,6,7,8,9 and 10. so how can i start my section number with 0 number and then as it is .

第二个问题: titleForHeaderInSection 委托方法中,tableView的所有部分都重复3次.在当前的ViewController上,我向上或向下滚动UITableView,然后该部分显示为正常(0到11),在重复该部分3次之后,该UITableView的正常部分显示出来.

second problem : in titleForHeaderInSection delegate method my all sections of tableView is repeat 3 times. and at current ViewController i scrolling UITableView up or down then section display as normal (0 to 11 ) this normal section of UITableView display after repetition of section 3 times.

例如:

我在 titleForHeaderInSection 委托方法中使用 NSLog(@%d",section); ,然后按如下所示显示在控制台中

I use NSLog (@" %d ", section ); in titleForHeaderInSection delegate method then display out put in console as as below

控制台输出:

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部分?

First Problem : i have question that how can i start my UITableView section with number 0 from multiple section ?

我认为您之所以惊慌,是因为这些操作不是严格按顺序进行的.这就是它发生的方式,您无法更改此行为.显然,您的部分在应用程序中以正确的顺序显示,但是它们恰好不会被顺序调用.这只是事件驱动的编程的本质.但是,我发现在您的代码中没有任何东西会因执行顺序而中断,但是也许您的代码中没有包含某些需要严格顺序调用这些方法的内容,如果是这样,您将必须重构该代码.但是在上面的代码片段中,我什么都没看到. UITableViewDataSource 委托方法的性质意味着,您可以(并且应该)以不依赖于调用单元格或节的顺序的方式来编写方法.永远不要依靠UI中的事件顺序来控制如何构建视图的基础模型.

I assume your consternation stems from the fact that these aren't invoked in strictly sequential fashion. That's just how it happens to work and you can't change this behavior. Clearly, your sections appear in the right order in the app, but they just don't happen to be invoked sequentially. That's just the nature of event-driven programming. But, I see nothing in your code that breaks on the basis of this order of execution, but perhaps there's something in your code that you haven't included here that requires a strictly sequential invocation of those methods, and if so, you'll have to refactor that code. But I'm seeing nothing like that in the above code snippets. The nature of the UITableViewDataSource delegate methods mean that you can (and should) write your methods in such a manner that they're not dependent upon the order that the cells or sections are invoked. Never rely on the sequence of events from the UI to govern how you build your view's underlying model.

第二个问题:在titleForHeaderInSection委托方法中,tableView的所有部分都重复3次.在当前的ViewController上,我向上或向下滚动UITableView,然后将部分重复显示3次后,部分显示为正常(0到11),而该UITableView的正常部分显示.

second problem : in titleForHeaderInSection delegate method my all sections of tableView is repeat 3 times. and at current ViewController i scrolling UITableView up or down then section display as normal (0 to 11 ) this normal section of UITableView display after repetition of section 3 times.

如果它被连续三次调用,我会感到惊讶.我敢打赌(特别是鉴于您的日志显示了格式稍有不同的 NSLog 语句的证据),您在代码中的某处还有其他 NSLog 语句.我会尝试将您的 NSLog 语句更改为类似以下内容:

I'd be surprised if it's being called three times in succession. I'd wager (especially given your log shows evidence of slightly different formatted NSLog statements) that you have other NSLog statements in your code somewhere. I'd try changing your NSLog statement to something like:

NSLog(@"%s section=%d", __FUNCTION__, section);

通过这种方式,(a)您知道从哪个方法记录日志;(b)通过在格式字符串中添加描述性的"section =" ,这意味着如果您永远不会对正在记录的数字感到困惑.我敢打赌,这三个 NSLog 语句并非全部来自您的 titleForHeaderInSection .就我个人而言,我不再将 NSLog 语句放在没有 __ FUNCTION __ 引用的代码中,因为很容易混淆是什么生成了什么 NSLog 声明.

This way, (a) you know which method it's being logged from; and (b) by adding the descriptive "section=" in the formatting string, it means that if you will never be confused about what numbers are being logged. I'd be willing to bet that not all of these three NSLog statements are from your titleForHeaderInSection. Personally, I no longer put NSLog statements in my code without the __FUNCTION__ reference, as it's just too easy to get confused as to what's generating what NSLog statement.

但是即使 titleForHeaderInSection 被多次调用,那又如何呢?iOS已经在幕后进行了优化,可以处理各种事情.了解我们正在发生的事情对我们很有帮助(例如,确保您不会在 titleForHeaderInSection 中执行一些愚蠢的事情,例如一些计算密集型或网络密集型活动),但这是拥抱宁静祈祷,并感谢我们作为开发人员可以控制的东西,以及我们没有控制的东西.(并且 UITableViewDataSource 调用的顺序和数量是我们无法控制的那些事情之一.)只要您不是方法的多余调用的来源(例如,执行不必要的 reloadData 调用),我不必担心.

But even if titleForHeaderInSection is called multiple times, so what? iOS has been optimized behind the scenes to do all sorts of stuff. It's good for us to understand what's happening (e.g. to make sure you don't do something silly like some computationally intensive or network intensive activity in titleForHeaderInSection), but this is a time to embrace the serenity prayer and appreciate what we as developers have control over, and that which we don't. (And the order and number of the UITableViewDataSource calls is one of those things we don't control.) As long as you're not the source of redundant calls to the methods (e.g. doing unnecessary reloadData calls), I wouldn't worry about it.

总结一下,虽然 titleForHeaderInSection 连续被调用了三次会令我感到惊讶,但不要依赖于它仅被调用一次.我认为表格视图可能会针对表格的每个部分调用一次(可能要进行某种操作,例如找出整个表格的高度,以便滚动条的大小会适当调整),然后针对屏幕上可见的部分再次调用它(以实际显示节标题).

To sum up, while I'd be surprised if titleForHeaderInSection is being called three times consecutively, don't rely on it being called only once. I think table views may call it once for each section of the table (probably to do something like figure out the height of the whole table so the scroll bar will be sized appropriately) and then once again for the sections that are visible on the screen (to actually show the section headers).

这篇关于当我有多个部分时,如何以数字0开头UITableView部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆