创建自定义tableview分隔符时出错 [英] error creating custom tableview separator
本文介绍了创建自定义tableview分隔符时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用自定义分隔符创建一个表视图.我制作了一个自定义单元格,用于保存内容,而另一个单元格仅包含带有分隔符的UIImageView. 问题在于如何访问内容. 我不能使用indexPath.row
I'm trying to create a tableview with custom separator. I made a custom Cell that holds the content, and another that contains only a UIImageView with the separator. The problem is in how content is being accessed. I can not use the indexPath.row
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier: @"CustomCell"];
if(cell == nil)
{
cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil] objectAtIndex:0];
}
SeparatorCell *cellSeparator = (SeparatorCell *) [tableView dequeueReusableCellWithIdentifier: @"SeparatorCell"];
if(cellSeparator == nil)
{
cellSeparator = [[[NSBundle mainBundle] loadNibNamed:@"SeparatorCell" owner:self options:nil] objectAtIndex:0];
}
if(indexPath.row % 2)
{
UIFont * myCustomFont = [UIFont fontWithName:@"Arial Black" size:16];
[cell.titulo setFont:myCustomFont];
cell.titulo.textColor = [UIColor colorWithRed:103/255.0f green:169/255.0f blue:0/255.0f alpha:1];
cell.titulo.text = [Title objectAtIndex:myRow];
cell.subtitle.text = [Subtitle objectAtIndex:myRow];
cell.tag = myRow;
myRow++;
}
else [cellSeparator.imagem setImage:[UIImage imageNamed:@"CustomSeparator.png"]];
if(indexPath.row % 2) return cell;
else return cellSeparator;
}
myRow
myRow
是全局NSInteger.
is a global NSInteger.
我已经尝试过
if(myRow == Title.count) myRow=0;
else myRow++;
推荐答案
尝试一下:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSInteger numOfRowsIncludeSeparator = 0;
if (self.model.count > 0) {
NSInteger numOfSeperators = self.model.count - 1;
numOfRowsIncludeSeparator = self.model.count + numOfSeperators;
}
return numOfRowsIncludeSeparator;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ContentCellIdentifier = @"ContentCell";
static NSString *SeparatorCellIdentifier = @"SeparatorCell";
UITableViewCell *cell = nil;
if (indexPath.row % 2 == 0) {
// this is a content cell
cell = [tableView dequeueReusableCellWithIdentifier:ContentCellIdentifier];
if(cell == nil)
{
cell = [[[NSBundle mainBundle] loadNibNamed:@"ContentCell" owner:self options:nil] objectAtIndex:0];
}
// get the model index
NSInteger indexInModel = indexPath.row / 2;
// get the model for this row
NSString *modelObject = [self.model objectAtIndex:indexInModel];
// Configure the cell...
cell.textLabel.text = modelObject;
}
else {
// this is a separator cell
cell = [tableView dequeueReusableCellWithIdentifier:SeparatorCellIdentifier];
if(cell == nil)
{
cell = [[[NSBundle mainBundle] loadNibNamed:@"SeparatorCell" owner:self options:nil] objectAtIndex:0];
}
}
return cell;
}
不要忘记将ContentCell.xib的IB中的Identifier
字段设置为"ContentCell",将SeparatorCell.xib设置为"SeparatorCell".
Don't forget to set the Identifier
field in IB of the ContentCell.xib to be "ContentCell" and the SeparatorCell.xib to be "SeparatorCell".
这篇关于创建自定义tableview分隔符时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文