我想根据条件将细胞分组,但我错了重复值。
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 5;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section == 0)
{
return 1;
}
else if(section == 1)
{
return 6;
}
else if(section ==2)
{
return 2;
}
else
{
return [arrControls count]-9;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.textLabel.text = [arrControls objectAtIndex:indexPath.row];
return cell;
}
答案 0 :(得分:0)
请详细说明你的问题。很难理解你的问题到底是什么。
如果您正在为tableCell使用xib或storyboard,请使用此方法1:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MyCellIdentifier";
MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
cell = (MyCell *)[nib objectAtIndex:0];
}
如果您没有使用故事板进行单元格尝试此方法2:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MyCellIdentifier";
UITableViewCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell setNeedsDisplay];
}
希望这会对你有帮助..
答案 1 :(得分:0)
为每个部分使用array
NSMutableArray
个。
NSMutableArray *arrays[5];
for (int i = 0; i < 5;i++)
{
arrays[i] = [[NSMutableArray alloc]init];
}
[arrays[0] addObject:@"add Your objects in section 0"];
[arrays[1] addObject:@"add Your objects in section 1"];
[arrays[2] addObject:@"add Your objects in section 2"];
等。
然后
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
array[section].count
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 5;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.textLabel.text = [array[indexPath.section] objectAtIndex:indexPath.row];
return cell;
}
答案 2 :(得分:0)
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(inderPath.Section ==0 )
{
//assuing that custom class name is detailTableViewCell.h and interface file name is same .
static NSString *simpleTableIdentifier = @detailTableViewCell ;
detailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]
if(cell == nil )
{
NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"detailTableViewCell" ownder self options:nil];
cell = [nib objectAtIndex:0]
}
return cell ;
}
if(inderPath.Section ==1 )
{
similarly for another subclass of UItableViewCell ;
}
if(inderPath.Section ==2 )
{
similarly for another subclass of UItableViewCell ;
}
// one common cell for other sections
NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.textLabel.text = [arrControls objectAtIndex:indexPath.row];
return cell;
}
答案 3 :(得分:0)
试试这个`
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * cellIdentifier = @&#34; cell&#34 ;;
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(indexPath.section == 0)
{
cell.textLabel.text = [firstArray objectAtIndex:indexPath.row];
}
否则if(indexPath.section == 1)
{
cell.textLabel.text = [secondArray objectAtIndex:indexPath.row];
}
否则if(indexPath.section == 2)
{
cell.textLabel.text = [thirdArray objectAtIndex:indexPath.row];
}
否则if(indexPath.section == 3)
{
cell.textLabel.text = [thirdArray objectAtIndex:indexPath.row];
}
其他
{
cell.textLabel.text = [FourthArray objectAtIndex:indexPath.row];
}
返回单元;
}
`
答案 4 :(得分:0)
@Dalvik。每个部分需要不同的数组。你已经只有一个mutableArray。
您可以使用数组中的每个对象为行提供数组。
您可以使用的数据格式如下
( {key:Array}, {key:Array}, {key:Array}, {key:Array}, )
或只是
( (阵列), (阵列), )
inside,numberOfRowsInSection:你可以使用它来访问它 { return [[[dataArray objectAtIndex:section] ObjectFoyKey:key] count];
or
return [[dataarray objectAtIndex:section]count];
}
inside,cellFOrRowAtIndexPath:你可以使用它来访问它 {
array = [[dataArray objectAtIndex:section]ObjectFoyKey:key];
cell.textlabel.text = [array objectAtIndex:indexPath.row];
or
array = [dataArray objectAtIndex:section];
cell.textlabel.text = [array objectAtIndex:indexPath.row];
}
希望这会对你有帮助..