【问题标题】:How can i start my UITableView section with number 0 when i have multiple sections?当我有多个部分时,如何以数字 0 开始我的 UITableView 部分?
【发布时间】:2012-09-17 07:51:11
【问题描述】:

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

我有关于 UITableView 部分的问题:

第一个问题:我有一个问题,如何从多个部分开始我的 UITableView 部分,编号为 0?

例如:

我在UITableViewnumberOfSectionsInTableView 委托方法中有 11 个部分,我的部分首先从第 11 个部分开始,然后是 0、1、2、3、4、5、6、7、8, 9 和 10。 那么我怎样才能以 0 号开始我的节号,然后按原样。

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

例如:

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

控制台输出:

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];
}

提前致谢

【问题讨论】:

    标签: iphone objective-c ios xcode uitableview


    【解决方案1】:

    你问:

    第一个问题:我有一个问题,我如何从多个部分开始我的 UITableView 部分,编号为 0?

    我认为您的惊愕源于这些不是以严格的顺序方式调用的事实。这就是它的工作方式,你无法改变这种行为。显然,您的部分在应用程序中以正确的顺序出现,但它们只是碰巧没有按顺序调用。这就是事件驱动编程的本质。但是,我在您的代码中没有看到任何基于此执行顺序的中断,但是您的代码中可能有一些您没有包含在此处的内容需要严格按顺序调用这些方法,如果是这样,您将必须重构该代码。但是我在上面的代码 sn-ps 中没有看到类似的东西。 UITableViewDataSource 委托方法的性质意味着您可以(并且应该)以不依赖于调用单元格或部分的顺序的方式编写您的方法。永远不要依赖 UI 中的事件序列来控制您如何构建视图的底层模型。

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

    如果它被连续调用 3 次,我会感到惊讶。我敢打赌(特别是考虑到您的日志显示格式略有不同的NSLog 语句的证据)您的代码中的某处还有其他NSLog 语句。我会尝试将您的 NSLog 声明更改为:

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

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

    但是即使titleForHeaderInSection 被多次调用,那又如何呢? iOS 已经在幕后进行了优化,可以做各种各样的事情。了解正在发生的事情对我们有好处(例如,确保您不会做一些愚蠢的事情,例如 titleForHeaderInSection 中的一些计算密集型或网络密集型活动),但现在是拥抱 serenity prayer 并欣赏我们的时候了开发人员可以控制,而我们没有。 (UITableViewDataSource 调用的顺序和数量是我们无法控制的事情之一。)只要您不是方法冗余调用的来源(例如,进行不必要的reloadData 调用),我不会担心的。

    总而言之,如果titleForHeaderInSection 被连续调用 3 次,我会感到惊讶,但不要指望它只被调用一次。我认为表格视图可能会为表格的每个部分调用一次(可能会执行一些操作,例如计算整个表格的高度,以便适当调整滚动条的大小),然后再为屏幕上可见的部分调用一次(实际显示部分标题)。

    【讨论】:

    • 感谢您的帮助...但好消息是我解决了EXC_BAD_ACCESS (Code = 1, address = 0x30000008) issue. :) 这是内存管理问题。它是在您释放已释放对象的内存时生成的......谢谢朋友:)
    • 点赞“UITableViewDataSource 委托方法的性质意味着您可以(并且应该)以不依赖于调用单元格或部分的顺序的方式编写方法. 永远不要依赖 UI 中的事件序列来控制您如何构建视图的底层模型。”
    【解决方案2】:
    -(NSInteger)numberOfRowsInTotal
    {
    
      NSInteger sections = self.numberOfSections;
      NSInteger cellCount = 0;
      for (NSInteger i = 0; i < sections; i++) 
     {
        cellCount += [self numberOfRowsInSection:i];
     }
    
    return cellCount;
     }
    

    【讨论】:

    • 这是什么?请详细解释:(这个方法我需要在哪里实现?
    • 我敢打赌,Jerry 假设您遇到了问题,因为您的代码依赖于被调用一次且仅一次的 UITableViewDataSource 方法。如果您的方法将行加起来,他的解决方案似乎正在解决问题(如果方法被多次调用,那么就会变得太多)。我没有在您的代码中看到这个特殊问题,但这是一个示例,说明如果您这样做了,您将如何解决该问题。不知道他为什么跳到这个特定的例子,但也许这是他在代码中遇到的那种问题的一个例子,因此是他的解决方案。
    【解决方案3】:

    这不是正确的方法,但您可以使用下面的代码并且它的工作完美。

    [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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      相关资源
      最近更新 更多