[英]Objective-C Sectioned UITableView
我想要一個分段的UITableView但我不知道如何,我的情況:
我有2個數組
數組日期(“ 12/08/13”,“ 13/08/13”,“ 17/08/13”)數組Count(“ 2”,“ 1”,“ 5”)
計數表示該部分中有多少行。
我有
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [dateArray objectAtIndex:section];
}
但是如何在第一部分中使第二部分中的兩行成為第一行,而在第三部分中使第二行成為五行。
我希望有一個人可以幫助我。 如果您有任何疑問,請問。
謝謝。
編輯:我從我的網站上獲取我的信息,所以陣列每天都在變化。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// length of your section count array:
return [sectionCountArray count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// the section count from your array, converted to an integer:
return [sectionCountArray[section] integerValue];
}
更新:從您的評論看來,您有一個“扁平的”數據源數組。 由於每個部分的行號均以零開頭,因此您必須根據行號和cellForRowAtIndexPath
中所有先前部分的部分計數來計算數組的索引:
NSInteger flatIndex = indexPath.row;
for (NSInteger sec = 0; sec < indexPath.section; sec++)
flatIndex += [tableView numberOfRowsInSection:sec];
cell.textLabel.text = yourFlatDataArray[flatIndex]; // (Just an example!)
您在numberOfSectionsInTableView:
返回節數numberOfSectionsInTableView:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [self.countArray count];
}
然后從數組中獲取數字,並在numberOfRowsInSection:
返回該數字numberOfRowsInSection:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSNumber *count = [self.countArray objectAtIndex:section];
return [count integerValue];
}
您需要重寫以下UITableView數據源方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0)
return 2
else if (section == 1)
return 1
else (section == 2)
return 5
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.