【问题标题】:method to add custom cell添加自定义单元格的方法
【发布时间】:2012-04-16 12:29:46
【问题描述】:

我想向我的 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

感谢您的宝贵时间

【问题讨论】:

  • 第二次调用addInfo不是覆盖了第一次调用吗?
  • 不应该是不同的单元格吗?
  • 正如@Nit 已经指出的那样,您需要某种 logic 来确定用什么填充单元格 - 您总是对所有单元格做同样的事情,因此你看到的结果。
  • @Hosni 访问 Apple 开发者网站并阅读他们关于模型视图控制器的文档并查看 UITableView 的示例代码。如果你继续你正在做的事情,即使你得到了这个工作(使用下面的答案),那么你将来会让自己的事情变得更加困难。当前的答案集仅显示如何获得您当前的工作。他们没有展示正确的做法

标签: iphone objective-c class customization


【解决方案1】:
[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"];

【讨论】:

  • 这实际上仅适用于这种情况,但在我使用时无效:[cell addInfo:@"journal 1":@"description 1":@"01/02/2012":@"second .png"]; [单元格添加信息:@“期刊 2”:@“描述 2”:@“01/02/2012”:@“second.png”];
  • U 会得到重复的数据,因为第二次调用会覆盖第一次
  • @Hosni 另一个选项是在字典中添加数据并将其添加到数组中,因此之后您必须提供 objectAtIndex valueForkey 。因此它将根据数组索引显示数据。
  • @Chinttu 你能详细回答一下吗
【解决方案2】:

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

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

 }

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-26
    • 2020-09-25
    相关资源
    最近更新 更多