How to initialize a custom prototype style table cell in iOS 5 storyboards?

前端 未结 5 1481
悲&欢浪女
悲&欢浪女 2020-12-23 17:01

I am converting over to iOS 5 and storyboards. When I have a table view with default cell style, everything works fine.

- (UITableViewCell *)tableView:(UITab         


        
相关标签:
5条回答
  • 2020-12-23 17:13

    This worked perfect for me (Xcode 4.5.2 and iOS 6.0):

    -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    
        if( cell == nil){
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        }
    
        UILabel *title = (UILabel*) [cell viewWithTag:1000];
        UILabel *summary = (UILabel*) [cell viewWithTag:1001];
        [title setText:[ tableMainTitle objectAtIndex:indexPath.row]];
        [summary setText:[ tableSubTitle objectAtIndex:indexPath.row]];
        return cell;
    }
    

    Important: Do not forget setting delegate and datasource.

    0 讨论(0)
  • 2020-12-23 17:19

    If you load your view controller that contains the tableview using:

    MyViewController *myViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];
    

    Then inside cellForRowAtIndexPath you just need one line:

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifierFromStoryboard"];
    

    dequeueReusableCellWithIdentifier will instantiate one cell if there is none exists.

    0 讨论(0)
  • 2020-12-23 17:31

    This way

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"Cell";
        CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
    
        return cell;
    }
    
    0 讨论(0)
  • 2020-12-23 17:32

    I think this one works very well. I was trying to get my head around it for sometime and thanks it worked.. well i was doing this, when trying to alloc a cell i was allocating using the general cell class instead of the one i had created

    so i was doing this, so thats reason why it did not work

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    

    so instead of UITableViewCell alloc the cell with your class, well with the above example "CustomCell"

    Thanks again

    0 讨论(0)
  • 2020-12-23 17:37
    static NSString *identifier=@"SearchUser";
    SearchUserCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil)
    {
        cell=[[SearchUserCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    return cell;
    
    0 讨论(0)
提交回复
热议问题