我在表格中有3个部分,因此我只想在第1部分(第2行和第3行)中为我的标题单元格指定值,以便标题出现在下一部分中并在每个单元格中跳转。帮我解决一下! 这是我的代码:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
//------------ Customise lable and image in cell ----------------------
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
self.nameModuleLable = [[UILabel alloc] initWithFrame:CGRectMake(35, 5, 80, 30)];
self.nameModuleLable.font = [UIFont systemFontOfSize:13];
self.nameModuleLable.textColor = [UIColor whiteColor];
self.nameModuleLable.tag = 1;
self.nuberOfMessLable = [[UILabel alloc] initWithFrame:CGRectMake(200, 0, 40, 40)];
self.nuberOfMessLable.font = [UIFont systemFontOfSize:13];
self.nuberOfMessLable.textColor = [UIColor whiteColor];
self.nuberOfMessLable.tag = 2;
self.cellimageView = [[UIImageView alloc] init];
self.cellimageView.frame = CGRectMake(8, 11, 20, 20);
self.cellimageView.tag = 3;
[cell addSubview:self.nameModuleLable];
[cell addSubview:self.cellimageView];
[cell addSubview:self.nuberOfMessLable];
}
self.nameModuleLable = (UILabel*)[cell viewWithTag:1];
self.nuberOfMessLable = (UILabel*)[cell viewWithTag:2];
self.cellimageView = (UIImageView*)[cell viewWithTag:3];
// -------------Number of section in cell 2 and 3 in section 0-----------
if (indexPath.section == 0) {
if (indexPath.row == 2) {
if (!numberOfMessage) {
self.nuberOfMessLable.text = @"0";
} else {
self.nuberOfMessLable.text = [NSString stringWithFormat:@"%d",numberOfMessage];
}
}
else if (indexPath.row == 3) {
if (!numberOfFriend) {
self.nuberOfMessLable.text = @"0";
} else {
self.nuberOfMessLable.text = [NSString stringWithFormat:@"%d",numberOfFriend];
}
}
//-------------------Assign value for home's menu------------------------------
self.nameModuleLable.text = [home objectAtIndex:indexPath.row];
self.cellimageView.image = [UIImage imageNamed:[homeImage objectAtIndex:indexPath.row]];
//--------------------------Assign value for application's menu--------------------------
} else if (indexPath.section == 1){
Module *currentModule = [self.moduleArray objectAtIndex:indexPath.row];
self.nameModuleLable.text = [currentModule valueForKey:@"mName"];
NSString *avaImageString = [currentModule valueForKey:@"mImage"];
NSURL *urlAva = [NSURL URLWithString:avaImageString];
NSData *dataAva = [NSData dataWithContentsOfURL:urlAva];
self.cellimageView.image = [UIImage imageWithData:dataAva];
//---------------------------Logout section---------------------------------
} else if (indexPath.section == 2) {
self.nameModuleLable.text = [logout objectAtIndex:indexPath.row];
self.cellimageView.image = [UIImage imageNamed:[logoutImage objectAtIndex:indexPath.row]];
}
return cell;
}
答案 0 :(得分:0)
以下是一些建议:
创建UITableViewCell的自定义子类(如named:CustomCell),其中包含nameModuleLable nuberOfMessLable和cellimageView。在CustomCell的init方法初始化上面的三个子视图。
更改tableView:cellForRowAtIndexPath:方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
//------------ Customise lable and image in cell ----------------------
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// -------------Number of section in cell 2 and 3 in section 0-----------
if (indexPath.section == 0) {
if (indexPath.row == 2) {
if (!numberOfMessage) {
cell.nuberOfMessLable.text = @"0";
} else {
cell.nuberOfMessLable.text = [NSString stringWithFormat:@"%d",numberOfMessage];
}
}
else if (indexPath.row == 3) {
if (!numberOfFriend) {
cell.nuberOfMessLable.text = @"0";
} else {
cell.nuberOfMessLable.text = [NSString stringWithFormat:@"%d",numberOfFriend];
}
}
//-------------------Assign value for home's menu------------------------------
cell.nameModuleLable.text = [home objectAtIndex:indexPath.row];
cell.cellimageView.image = [UIImage imageNamed:[homeImage objectAtIndex:indexPath.row]];
//--------------------------Assign value for application's menu--------------------------
} else if (indexPath.section == 1){
Module *currentModule = [self.moduleArray objectAtIndex:indexPath.row];
cell.nameModuleLable.text = [currentModule valueForKey:@"mName"];
NSString *avaImageString = [currentModule valueForKey:@"mImage"];
NSURL *urlAva = [NSURL URLWithString:avaImageString];
NSData *dataAva = [NSData dataWithContentsOfURL:urlAva];
cell.cellimageView.image = [UIImage imageWithData:dataAva];
//---------------------------Logout section---------------------------------
} else if (indexPath.section == 2) {
cell.nameModuleLable.text = [logout objectAtIndex:indexPath.row];
cell.cellimageView.image = [UIImage imageNamed:[logoutImage objectAtIndex:indexPath.row]];
}
return cell;
}
为了为您的标题单元格指定值,请在ViewController创建一些NSString来保存标题值。