2011-05-13 59 views
0

我花了最后3个小时在这个问题上挠头,因为一切似乎都正确,除了我无法通过我的tableviewcell类来设置uilabels在我的定制单元格。请求在customtableviewcell中加载成员错误,并设置其IBOUTLETS

这里是我的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
     static NSString *CellIdentifier = @"TableViewCell"; 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if (cell == nil) { 
      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil]; 
     cell = (UITableViewCell *)[nib objectAtIndex:0]; 


      //cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; 
     } 

     //cell.imageViewSection = 

     // Set up the cell 
     int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1]; 

     NSString *textTitle = [[stories objectAtIndex: storyIndex] objectForKey: @"title"]; 

     NSURL *imageurl = [NSURL URLWithString:[[stories objectAtIndex: storyIndex] objectForKey: @"description"]]; 
     UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:imageurl]]; 



     cell.imageViewSection = image; 
     cell.titleLabelText.text = textTitle; 

return cell; 

} 

如果有人能帮助我走出这将是真棒:)

回答

1

你不应该依赖于总是被在数组中的索引0返回对象的细胞。做一个简单的循环来找到实际的细胞。

并添加类型转换为电池子类使用

-(UITableViewCell*)tableView:(UITableView*)tableView 
     cellForRowAtIndexPath:(NSIndexPath*)indexPath 
{ 
    static NSString* cellID = @"cellID"; 
    MyCell* cell = (id)[tableView dequeueReusableCellWithIdentifier:cellID]; 
    if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" 
                owner:self 
                options:nil]; 
     for (cell in nib) { 
      if ([cell isKindOfClass:[MyCell class]]) { 
       break; 
      } 
     } 
    } 
    // Safely do tuff to cell 
    return cell; 
} 

这段代码假定细胞至少可用,如果没有返回表格视图单元格的行为是不确定的。

+0

谢谢你的抬头。我在我名为TableViewCell的tableviewcell类中设置了iboutlets。 当谈到使用 cell.imageViewSection.image =图像 我得到一个请求成员的东西不是一个结构或联合... 定制细胞一旦对象被初始化我就应该能够设置它的值... – MrPink 2011-05-13 13:20:30

+0

@MrPink - 啊,这是因为'UITableViewCell'没有你设置的属性。你需要强制转换。我延伸我的答案。水库神们的任何变化MrPink是你吗? – PeyloW 2011-05-13 13:30:52

+0

@MrPink - 还使用“UINib”实例来创建单元格。这个想法是一样的,但'UINib'允许将nib文件缓存在内存中以加快创建速度。 – PeyloW 2011-05-13 13:35:41

0

您正在将TableViewCell分配并投射到UITableViewCell变量,而UITableViewCell没有这些插座。相反,你应该做以下事情..

TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil]; 
    cell = (TableViewCell *)[nib objectAtIndex:0]; 
    } 
相关问题