2013-03-01 28 views
28

Tôi muốn làm cho tableView với nhiều phần nhưng tôi không muốn sử dụng từ điển tôi có hai mảng tôi muốn mảng đầu tiên nên được tải trong phần đầu tiên và thứ hai trong phần thứ hai.UITableView Với nhiều phần

Tôi có arrayOne với 3 mục và arrayTwo với 4 mục để biết cách thêm chúng và hiển thị chúng trong các phần.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if(section == 0) 
     return resultArray.count; 
    else 
     return resultArray.count; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 2; 
} 


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    NSLog(@"Number of Sections"); 
    if(section == 0) 
     return @"Section 1"; 
    if(section == 1) 
     return @"Section 2"; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSLog(@"Table Cell Data"); 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
    } 

    appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate]; 

    if (indexPath.section==0) {  
     appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate]; 
     ObjectData *theCellData = [resultArray objectAtIndex:indexPath.row]; 
     NSString *cellValue =theCellData.category; 
     NSLog(@"Cell Values %@",cellValue); 
     cell.textLabel.text = cellValue; 
     return cell; 
    } 
    else { 
     ObjectData *theCellData = [resultArray objectAtIndex:indexPath.row]; 
     NSString *cellValue =theCellData.category; 
     cell.textLabel.text = cellValue; 
     return cell;  
    } 
} 
+1

Sự cố bạn đang gặp phải là gì? Mã của bạn có vẻ đúng. – Rushi

+0

giá trị gán trong ô cho phần lỗi phần không được khai báo –

+0

Bạn phải sử dụng indexPath.section và không chỉ phần trong cellForRowAtIndexPath. Phần còn lại của mã của bạn là tốt. – Rushi

Trả lời

80
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
     return 2 ; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
     if (section==0) 
     { 
      return [array1 count]; 
     } 
     else{ 
      return [array2 count]; 
     } 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
     if(section == 0) 
      return @"Section 1"; 
     else 
      return @"Section 2"; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

     static NSString *CellIdentifier = @"Cell"; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
     } 

if (indexPath.section==0) { 
    ObjectData *theCellData = [array1 objectAtIndex:indexPath.row]; 
    NSString *cellValue =theCellData.category; 
    cell.textLabel.text = cellValue; 
} 
else { 
    ObjectData *theCellData = [array2 objectAtIndex:indexPath.row]; 
    NSString *cellValue =theCellData.category; 
    cell.textLabel.text = cellValue; 
} 
    return cell;   
} 
+0

Điều này - (NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: (NSInteger) phần { nếu (indexPath.section == 0) { trả lại [số đếm mảng 1]; } khác { trả lại [số đếm mảng 1]; } } là sai. Bạn không thể lấy indexPath.section tại đây. – Rushi

+0

Vâng ur phải chỉnh sửa nhờ Rushi –

+0

nó không hiển thị giá trị trong các giá trị di động –

0

Thay đổi này if (section==0)

-if (indexPath.section==0)

Bạn phải thực hiện chức năng này:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if(section == 0) 
     return arrSection1.count; 
     else 
     return arrSection2.count; 
} 
+0

một lần nữa nó đưa ra lỗi tôi đã làm như thế này indexPath.section –

+0

@NaziaJan Kiểm tra mã đã chỉnh sửa – Rushi

+0

@NaziaJan bạn đã thực hiện chức năng này? – Rushi

0

bạn numberOfRowsInSection nên một cái gì đó như thế này:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (section == 0) { 
     return firstArray.count; 
    } else { 
     return secondArray.count; 
    } 
} 
0

Bạn đang đi đúng hướng gần như ngay ....

Chúng ta hãy xem xét hai mảng của bạn là arrOnearrTwo .....

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
     if(section == 0) 
     return [arrOne count]; 
     else 
     return [arrTwo count]; 
    } 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
     { 

     static NSString *CellIdentifier = @"Cell"; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
      } 

      if (indexPath.section==0) { 


     ObjectData *theCellData = [arrOne objectAtIndex:indexPath.row]; 
     NSString *cellValue =theCellData.category; 
     NSLog(@"cellValue = %@",cellValue); //To see the value of string cellValue 
     cell.textLabel.text = cellValue; 

     return cell; 

    } 

    else { 
     ObjectData *theCellData = [arrTwo objectAtIndex:indexPath.row]; 
     NSString *cellValue =theCellData.category; 
     cell.textLabel.text = cellValue; 

     return cell;   
    } 
} 
+0

nó đang đưa ra lỗi tại indexpath.section –

+0

tôi đoán P nên là vốn trong indexpath, .... đã chỉnh sửa điều đó. Bây giờ thử, nó sẽ hoạt động. –

+0

nó đang hoạt động nhưng không hiển thị giá trị ô –

0

TableView Đại biểu Phương pháp đã cung cấp cho các phần như : (NSInteger) phần .. vì vậy chúng tôi cũng có thể sử dụng switchCase

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
     switch (section) 
     { 
      case 0: 
       return self.arrMedia.count; 
       break; 
      case 1: 
       return 1; 
       break; 
     } 
     return 0; 
    } 
0

Dưới đây là có liên quan Swift 3 dòng:

func numberOfSections (in tableView: UITableView) -> Int { 
    ... 
    return numberOfSections 
} 

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { 
    //Not necessary, but will let you format the section headers. Example: 
    guard let header = view as? UITableViewHeaderFooterView else { return } 
    header.textLabel?.font = UIFont.boldSystemFont(ofSize: 24) 
    header.textLabel?.textColor = UIColor.darkGray 
    header.textLabel?.textAlignment = .center 
    header.textLabel?.frame = header.frame 
    view.tintColor = UIColor.white 
} 

func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) { 
    //Also not necessary, but clear footers help space out sections 
    view.tintColor = UIColor.clear 
} 

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { 
    //Spacing between sections: 
    return 25 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    ... 
    return sectionCount 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    //All your cell setup 
    ... 
    //I call this to get the absolute row number (disregarding sections) 
    let row = getAbsoluteRow_InCurrentSection(indexPath: indexPath) 
    //Now you can use the row number to grab a value from an array or CoreData object and use the value to populate your cell!! 
} 

func getAbsoluteRow_InCurrentSection(indexPath: IndexPath) -> Int { 
    var aggRows = 0 
    if currentListEntity == "GrocTask" { 
     let curSection = indexPath.section 
     for i in 0..<curSection { 
      aggRows += flex1ArrayCnt[i] 
     } 
     aggRows += indexPath.row 
    } 
    return aggRows 
} 

// Xin lưu ý, tôi chỉ bao gồm những người có liên quan trực tiếp đến việc xây dựng một UITableView với các bộ phận. Tôi không bao gồm editActions, didSelect, hoặc các hàm đại biểu UITableView khác.

Các vấn đề liên quan