2012-07-12 52 views
2

我想创建一个带有自定义分隔符的tableview。我制作了一个自定义Cell来保存内容,另一个只包含带有分隔符的UIImageView。 问题在于如何访问内容。 我不能使用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

是一个全球性的NSInteger的。

我已经尝试过

if(myRow == Title.count) myRow=0; 
     else myRow++; 
+0

当你使用它时会发生什么? – Dustin 2012-07-12 14:22:40

+0

我在tableview中往下走,一行消失,当我再次看到那一行时,Xcode给了我一个错误,试图访问数组中不存在的对象,我相信变量> myRow 没有重置。 – darkman 2012-07-12 14:36:00

+0

而不是使用变量'myRow',为什么不使用'indexPath.row%2'? – Dustin 2012-07-12 14:37:59

回答

8

试试这个:

- (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; 
} 

不要忘记设置Identifier字段中ContentCell.xib的IB是 “ContentCell” 和SeparatorCell.xib成为“SeparatorCell”。

+0

好的解决方案。 – Dustin 2012-07-12 15:58:22

+0

感谢您的解决方案。工程很好:) – Aniruddha 2014-09-11 06:37:11