Table view with custom cell (programmatically)
Asked Answered
K

3

6

So far, I used to create custom nibs to make my cell as I wanted but this time, the height of a cell will change from one to another so that I can't create a fixed-size cell's nib.

So I decided to create it programmatically ... Is the way below the good way to achieve it ?

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UILabel *pseudoAndDate = [[UILabel alloc] initWithFrame:CGRectMake(0.0,0.0,320.0,20.0)];
        [pseudoAndDate setTag:1];
        [cell addSubview:pseudoAndDate];
        [pseudoAndDate release];
    }

    CommentRecord *thisRecord = [comments objectAtIndex:[indexPath row]];

    UILabel *label = (UILabel *)[cell viewWithTag:1];
    [label setText:[NSString stringWithFormat:@"%@ | %@",thisRecord.author,thisRecord.date]];

    return cell;
}

or .. am i missing something here ? Cause so far it doesn't seem to work ;)

Thanks,

Gotye.

Karelia answered 27/3, 2010 at 11:49 Comment(0)
K
0

New link for custom UITableViewCell programmatically Apple Documentation UITableViewCell

Karelia answered 18/9, 2015 at 7:9 Comment(1)
Note that link-only answers are discouraged, SO answers should be the end-point of a search for a solution (vs. yet another stopover of references, which tend to get stale over time). Please consider adding a stand-alone synopsis here, keeping the link as a referenceRuggiero
T
0

Why create a label when you don't need to? Use the UITableViewCell's label.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    CommentRecord *thisRecord = [comments objectAtIndex:[indexPath row]];

    cell.textLabel.text = [NSString stringWithFormat:@"%@ | %@",thisRecord.author,thisRecord.date];

    return cell;
}
Tarshatarshish answered 10/8, 2013 at 22:43 Comment(0)
X
0

If your problem is that the height varies from cell to cell you can use the method:

https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:heightForRowAtIndexPath:

From UITableViewDelagate to achieve it

Xena answered 10/11, 2013 at 23:28 Comment(0)
K
0

New link for custom UITableViewCell programmatically Apple Documentation UITableViewCell

Karelia answered 18/9, 2015 at 7:9 Comment(1)
Note that link-only answers are discouraged, SO answers should be the end-point of a search for a solution (vs. yet another stopover of references, which tend to get stale over time). Please consider adding a stand-alone synopsis here, keeping the link as a referenceRuggiero

© 2022 - 2024 — McMap. All rights reserved.