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;
}
}
Sự cố bạn đang gặp phải là gì? Mã của bạn có vẻ đúng. – Rushi
giá trị gán trong ô cho phần lỗi phần không được khai báo –
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