添加自定义单元格的方法

时间:2012-04-16 12:29:46

标签: iphone objective-c class customization

我想在customCell.m中添加一个方法,它允许我在一行中添加有关单元格的信息,所以我添加了:

 -(void) addInfo:(NSString*) pTitre:(NSString*) pDescritption:(NSString*) pDate: (NSString*)pImage
{
    [[self titre] setText:pTitre];
    [[self description] setText:pDescritption];
    [[self date] setText:pDate];
    [[self couverture] setImage:[UIImage imageNamed:pImage]];
}

但是当我在mytableview.m中调用方法时如下:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3 ;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"firstviewcustomcellCell";

firstviewcustomcellCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

if (cell == nil)
{
    NSArray *topLevelObject=[[NSBundle mainBundle] loadNibNamed:@"firestViewCell" owner:nil options:nil];

            for(id currentObject in topLevelObject)
            {
                if([currentObject isKindOfClass:[firstviewcustomcellCell class]])
                {
                    cell=(firstviewcustomcellCell*) currentObject;
                    break;
                }
            }    
}
    [cell addInfo:
     @"journal 1":
     @"description 1":
     @"01/02/2012":
        @"second.png"];

    [cell addInfo:
     @"journal 2":
     @"description 2":
     @"01/02/2012":
     @"second.png"];

    return cell;

}

这就是它所显示的:

http://img213.imageshack.us/img213/2346/capturedcran20120416142.png

感谢您的时间

2 个答案:

答案 0 :(得分:0)

[cell addInfo:
 [NSString stringWithFormat:@"journal 1"]:
 [NSString stringWithFormat:@"description 1"]:
 [NSString stringWithFormat:@"01/02/2012"]:
    @"second.png"];

[cell addInfo:
 [NSString stringWithFormat:@"journal 2"]:
 [NSString stringWithFormat:@"description 2"]:
 [NSString stringWithFormat:@"01/02/2012"]:
 @"second.png"];

请注意您的代码的这一部分.. 那里没有条件。

而是像这样做

[cell addInfo:
 [NSString stringWithFormat:@"journal %i", indexpath.row]:
 [NSString stringWithFormat:@"description %i", indexpath.row]:
 [NSString stringWithFormat:@"01/02/2012"]:
    @"second.png"];

答案 1 :(得分:0)

您无法一次添加所有单元格信息...您必须一次添加一个单元格信息...如果您想要添加所有单元格信息一次添加而不是放置条件......就像..

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 return 3 ;
}

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:   (NSIndexPath *)indexPath
 {
  static NSString *MyIdentifier = @"firstviewcustomcellCell";

   firstviewcustomcellCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

   if (cell == nil)
 {    
  NSArray *topLevelObject=[[NSBundle mainBundle] loadNibNamed:@"firestViewCell" owner:nil options:nil];

        for(id currentObject in topLevelObject)
        {
            if([currentObject isKindOfClass:[firstviewcustomcellCell class]])
            {
                cell=(firstviewcustomcellCell*) currentObject;
                break;
            }
        }    
   }



if(indexPath.row == 1)
 {
  [cell addInfo:
 [NSString stringWithFormat:@"journal 1"]:
 [NSString stringWithFormat:@"description 1"]:
 [NSString stringWithFormat:@"01/02/2012"]:
    @"second.png"];
}
  else if(indexPath.row == 2)
 {

  [cell addInfo:
  [NSString stringWithFormat:@"journal 2"]:
 [NSString stringWithFormat:@"description 2"]:
 [NSString stringWithFormat:@"01/02/2012"]:
 @"second.png"];
 }

return cell;

 }

希望,它会帮助你......寒冷