Я использую 2 таблицы на одном экране, один из которых плоский, а второй - пользовательскую ячейку. Только один работает
NSString *cellIdentifier = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (tableView==self.monthTableVW)
{
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text=[self.monthArr objectAtIndex:indexPath.row];
cell.backgroundColor=[ UIColor colorWithRed:224.0f/255.0f green:188.0f/255.0f blue:113.0f/255.0f alpha:0.5f];
}
if (tableView==self.attendanceTBVW)
{
customeCell *cell1=[[customeCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
cell1=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil)
{
NSArray *cellArr=[[NSBundle mainBundle]loadNibNamed:@"customeCell" owner:self options:nil];
cell=[cellArr objectAtIndex:0];
}
cell1.dateLbl.text=[self.monthArr objectAtIndex:indexPath.row];
}
return cell;
}
Мне нужно одно табличное представление, чтобы использовать в качестве выпадающего меню, а второе - для отображения данных, например посещаемости TBVW, которая является customCell.
5 ответов
Сначала установите другой идентификатор ячейки для другой ячейки табличного представления в инспекторе идентичности раскадровки. затем
#import CustomTableViewCell.h
Затем перетащите свойство dateLbl IBoutlet в этот класс. Затем приведите пользовательскую ячейку к этому типу.
NSString *cellIdentifier = @"cellID1";
NSString *customCell = @"cellID2";
UITableViewCell *cell;
if (tableView==self.monthTableVW)
{
if (cell == nil)
{
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text=[self.monthArr objectAtIndex:indexPath.row];
cell.backgroundColor=[ UIColor colorWithRed:224.0f/255.0f green:188.0f/255.0f blue:113.0f/255.0f alpha:0.5f];
}
if (tableView==self.attendanceTBVW)
{
//customeCell *cell1=[[customeCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
//cell1=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil)
{
(CustomTableViewCell *)cell = [tableView dequeueReusableCellWithIdentifier:customCell];
NSArray *cellArr=[[NSBundle mainBundle]loadNibNamed:@"customeCell" owner:self options:nil];
cell=[cellArr objectAtIndex:0];
cell.dateLbl.text=[self.monthArr objectAtIndex:indexPath.row];
}
//cell1.dateLbl.text=[self.monthArr objectAtIndex:indexPath.row];
}
return cell;
}
Попытайся. Надеюсь, это сработает.
Можно ли создать другую ячейку для разных таблиц и вернуть ее ячейку индивидуально.
Поскольку обе таблицы разные, поэтому вы должны вернуть разные ячейки для обеих.
Сделайте это изменения в вашем коде.
if (tableView==self.monthTableVW) {
NSString *cellIdentifier = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text=[self.monthArr objectAtIndex:indexPath.row];
cell.backgroundColor=[ UIColor colorWithRed:224.0f/255.0f green:188.0f/255.0f blue:113.0f/255.0f alpha:0.5f];
return cell;
}
if (tableView==self.attendanceTBVW) {
NSString *cellIdentifier = @"customeCell";
customeCell *cell1=[[customeCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
cell1=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil) {
NSArray *cellArr=[[NSBundle mainBundle]loadNibNamed:cellIdentifier owner:self options:nil];
cell=[cellArr objectAtIndex:0];
}
cell1.dateLbl.text=[self.monthArr objectAtIndex:indexPath.row];
return cell;
}
Судя по вашему коду, вы возвращаете только ячейку для выпадающего меню. Вы не возвращаете cell1 для посещаемости TBVW. Пожалуйста, верните отдельные ячейки для обоих видов таблицы. Надеюсь, поможет.
UITableViewCell * raj=[[UITableViewCell alloc]init];
if (tableView==self.monthTableVW)
{
NSString *cellIdentifier = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text=[self.monthArr objectAtIndex:indexPath.row];
cell.backgroundColor=[ UIColor colorWithRed:224.0f/255.0f green:188.0f/255.0f blue:113.0f/255.0f alpha:0.5f];
raj= cell;
}
if (tableView==self.attendanceTBVW)
{
NSString *cellIdentifier = @"customeCell";
customeCell *cell1=[[customeCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
cell1=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell1==nil) {
NSArray *cellArr=[[NSBundle mainBundle]loadNibNamed:cellIdentifier owner:self options:nil];
cell1=[cellArr objectAtIndex:0];
}
cell1.dateLbl.text=[self.monthArr objectAtIndex:indexPath.row];
raj= cell1;
}
return raj;
Этот работал для меня.. большое спасибо
Попробуйте сделать эти изменения, проверьте это
if (tableView==self.monthTableVW) {
NSString *cellIdentifier = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text=[self.monthArr objectAtIndex:indexPath.row];
cell.backgroundColor=[ UIColor colorWithRed:224.0f/255.0f green:188.0f/255.0f blue:113.0f/255.0f alpha:0.5f];
return cell;
}
if (tableView==self.attendanceTBVW) {
NSString *cellIdentifier = @"customeCell";
customeCell *cell1=[[customeCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
cell1=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil) {
NSArray *cellArr=[[NSBundle mainBundle]loadNibNamed:cellIdentifier owner:self options:nil];
cell=[cellArr objectAtIndex:0];
}
cell1.dateLbl.text=[self.monthArr objectAtIndex:indexPath.row];
return cell;
}