我在表视图中有2个部分,我从一个数组加载了一个部分,从另一个数组加载了一个部分。如何设置两个部分的单元格值?
我正在使用以下代码:
标题部分标题。
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section
{
if(section == 0)
return @" Scheduled Patients ";
else
return @" Walk In Patients ";
}
部分行:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section==0)
return [resultArray count];
else {
EMRAppDelegate *appDelegate = (EMRAppDelegate *)[[UIApplication sharedApplication] delegate];
return [appDelegate.walkPatients count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIButton * uploadButton = nil;
static NSString * Identifier = @"Identifier";
UITableViewCell * cell = [table dequeueReusableCellWithIdentifier:Identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:Identifier] autorelease];
uploadButton = [UIButton buttonWithType:UIButtonTypeCustom];
[uploadButton setFrame:CGRectMake(10, 10, 24, 24)];
[uploadButton setImage:[UIImage imageNamed:@"upload.png"] forState:UIControlStateNormal];
[uploadButton addTarget:self action:@selector(onUploadButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[uploadButton setTag:999];
cell.accessoryView = uploadButton;
}
else {
uploadButton = (UIButton *)cell.accessoryView;
}
EMRAppDelegate *appDelegate = (EMRAppDelegate *)[[UIApplication sharedApplication] delegate];
ObjectData *theCellData =[resultArray objectAtIndex:indexPath.row];
WalkPatient * patient = [ appDelegate.walkPatients objectAtIndex:indexPath.row];
if (indexPath.section == 0)
{
// do coding for cell loading for Scheduled Patients
cell.textLabel.font = [UIFont systemFontOfSize:18];
cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", theCellData.firstName, theCellData.lasttName];
if (patient.syncDate != nil) {
UIImageView * checkmark = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];
[checkmark setFrame:CGRectMake(12, 0, 12, 12)];
[checkmark setTag:998];
[uploadButton addSubview:checkmark];
[checkmark release];
[uploadButton removeTarget:self action:@selector(onUploadButtonClick:) forControlEvents:UIControlEventTouchUpInside];
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy hh:mm"];
cell.detailTextLabel.font = [UIFont systemFontOfSize:12];
cell.detailTextLabel.text = [NSString stringWithFormat:@"Synched: %@", [dateFormatter stringFromDate:patient.syncDate]];
[dateFormatter release];
}
else {
UIView * checkmark = (UIView *)[uploadButton viewWithTag:998];
if (checkmark != nil) {
[checkmark removeFromSuperview];
}
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
else
{
// do load cells for Walk In Patients.
cell.textLabel.font = [UIFont systemFontOfSize:18];
cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", patient.firstName, patient.lastName];
if (patient.syncDate != nil) {
UIImageView * checkmark = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];
[checkmark setFrame:CGRectMake(12, 0, 12, 12)];
[checkmark setTag:998];
[uploadButton addSubview:checkmark];
[checkmark release];
[uploadButton removeTarget:self action:@selector(onUploadButtonClick:) forControlEvents:UIControlEventTouchUpInside];
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy hh:mm"];
cell.detailTextLabel.font = [UIFont systemFontOfSize:12];
cell.detailTextLabel.text = [NSString stringWithFormat:@"Synched: %@", [dateFormatter stringFromDate:patient.syncDate]];
[dateFormatter release];
}
else {
UIView * checkmark = (UIView *)[uploadButton viewWithTag:998];
if (checkmark != nil) {
[checkmark removeFromSuperview];
}
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
}
答案 0 :(得分:1)
简单。在cellForRowAtIndexPath
方法中,您还可以设置一个条件来检查该部分并根据它们加载数据。
if (indexPath.section == 0)
{
// do coding for cell loading for Scheduled Patients
UIButton *btnsection0=[UIButton buttonWithType:UIButtonTypeCustom];
[btnsection0 addTarget:self action:@selector(showProject:) forControlEvents:UIControlEventTouchUpInside];
}
else
{
// do load cells for Walk In Patients.
UIButton *btnsection1=[UIButton buttonWithType:UIButtonTypeCustom];
[btnsection0 addTarget:self action:@selector(showProject:) forControlEvents:UIControlEventTouchUpInside];
}
答案 1 :(得分:0)
你几乎得到了它。 cellForRowAtIndexPath方法附带一个indexPath参数,可以为您提供:
NSInteger row = indexPath.row;
NSInteger section = indexPath.section;
请始终始终使cellForRowAtIndexPath逻辑镜像numberOfRowsInSection逻辑,因此当您被要求输入行数时:
return (section==0)? [array0 count] : [array1 count];
...当您被问到一个单元格时,您需要该单元格的数据
NSInteger row = indexPath.row;
NSInteger section = indexPath.section;
NSArray *arrayForThisSection = (section==0)? array0 : array1;
id elementForThisRow = [arrayForThisSection objectAtIndex:row];
// config the cell with elementForThisRow
答案 2 :(得分:0)
来自documentation的UITableViewDataSource:
UITableView declares a category on NSIndexPath that enables you to get the represented row index (row property) and section index (section property),
所以你可以去indexPath.section
它会告诉你tableview要填充哪个部分。
答案 3 :(得分:0)
UIButton *btnsection0=[UIButton buttonWithType:UIButtonTypeCustom];
if (indexPath.section == 0)
{
btnsection0.tag = [NSString stringWithFormat:@"%d%d",indexPath.section,indexPath.row];
}else{
btnsection0.tag = [NSString stringWithFormat:@"%d%d",indexPath.section,indexPath.row];
}
[btnsection0 addTarget:self action:@selector(showProject:) forControlEvents:UIControlEventTouchUpInside];
按钮事件
-(IBAction)showProject:(id)sender{
NSString *s = [NSString stringWithFormat:@"%d%d",indexPath.section,indexPath.row];
int section = [[s substringToIndex:1] intValue];
int row = [[s substringFromIndex:0] intValue];
}