UITableView,androiduitableview
</pre><pre name="code" class="objc">- (void)loadView{ /** * UITableView:表視圖,是iOS中用來顯示以及能夠編輯一系列具有相同資料結構的資訊列表的牛逼控制項.(VIP). UITableView繼承自UIScrollView,所以能夠滑動,但是只能在垂直方向滑動,而且只有一列. UITableView是由分區(section,對應班級學生的分組),以及行(row,對應一個組的組員).對於分區和行的索引值都是從0開始的.如果要擷取某一個行(擷取班裡的某一個人),要根據分區以及所在分區的行(根據該學生所在的分組以及在組中的位置(第幾個人))決定.而這兩個資訊都儲存在NSIndexPath類型的對象中. UITableView的每一行都是一個UITableViewCell類型的對象,每一個cell上都包含imageView(left),label(textLabel,detailLabel,center),accessoryView(right) */ UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain]; //設定分割線的樣式// tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched; //設定分割線的顏色 tableView.separatorColor = [UIColor lightGrayColor]; //指定tableView的資料來源.(為tableView顯示提供資料支援). 需要服從UITableViewDataSource協議 tableView.dataSource = self; //設定tableView的代理,(用來處理cell的點擊事件). tableView.delegate = self; //設定tableView行高 tableView.rowHeight = 70; self.view = tableView; [tableView release];}- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view. self.navigationItem.title = @"9班";}#pragma mark - UITableViewDataSource//設定tableView的行數(每個分組的行數)- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{// //1.先擷取key值// NSString *key = self.titles[section];// //2.根據key值擷取對應的數組// NSArray *group = self.names[key];// //3.求數組元素個數// return [group count];// return [self.names[self.titles[section]] count]; return 1000;}//用來建立cell,每一行都要對應一個cell.- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //1.建立重用標識符 static NSString *identifier = @"7ban"; //2.根據重用標識符去重用隊列中取出可重用的cell. StudentCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; //3.判斷是否成功取到可重用的cell. if (!cell) { //如果沒有成功取到可重用的cell,就建立一個cell. //建立完cell之後一定要給cell貼一個重用標識符.方便別人重用. cell = [[[StudentCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier] autorelease]; } cell.textLabel.text = @"憨豆"; cell.detailTextLabel.text = @"熊貓"; return cell; // //1.先擷取key// NSString *key = self.titles[indexPath.section];// //2.根據key擷取value(數組)// NSArray *group = self.names[key];// //3.從數組中取出元素// NSString *name = group[indexPath.row];// cell.textLabel.text = [[self.names objectForKey:[self.titles objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];}//返回tableView的分區個數//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView//{// return [self.names count];//}////設定每個分區頭顯示的文字.(頁首)//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section//{// return self.titles[section];//}//設定tableView右邊的索引值,(用來快速定位分區,方便尋找),要和每個分區的title對應上//- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView//{// return self.titles;//}#pragma mark - UITableViewDelegate//設定行高- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ //偶數的cell高度為100,奇數的cell高度為50 if (indexPath.row % 2) { return 100; } return 50;}//當cell被選中時觸發- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"%d", indexPath.row);}
UITableView怎隱藏一個section
解決了,先把tableView.sectionHeaderHeight = 0;tableView.sectionFooterHeight = 0;再根據需要實現:- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section就可以控制group模式隱藏section了。
uitableview 怎擷取指定分割線 並且設定顏色
UITableView的分割線(separator)是私人類,應該是無法擷取的。
不過你可以通過tableView的屬性修改它:
UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(20, 20, 400, 300) style:UITableViewStylePlain]; tableView.separatorColor = [UIColor redColor]; tableView.separatorInset = UIEdgeInsetsMake(0,80, 0, 80); // 設定端距,這裡表示separator離左邊和右邊均80像素 tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; tableView.dataSource = self;
如此設定後的tableView是這樣的:(左右空出了80像素)
如果你想設定單個分割線的顏色,那就自己畫分割線吧。你可以用coreGraphics,也可以用UIView,這裡用UIView來畫:
UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(20, 20, 400, 300) style:UITableViewStylePlain]; tableView.separatorStyle = UITableViewCellSeparatorStyleNone; // 這樣就不會顯示內建的分割線 tableView.dataSource = self; for (int i = 0; i < 8; i++) { UIView * separator = [[UIView alloc] initWithFrame:CGRectMake(10, (i+1) * 40 /* i乘以高度*/, 380, 1)]; separator.backgroundColor = [UIColor colorWithRed:0.03 * i green:0.05*i blue:0.1*i alpha:1]; [tableView addSubview:separator]; } [self addSubview:tableView];效果:
滾動正常
當然,為了和諧,你應該把自訂的separator的間隔和cell的高度設為一致。