[英]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.