Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
612 views
in Technique[技术] by (71.8m points)

iphone - UITableViewCell Font Not Changing

I am trying to customize the font of a UITableViewCell using the following code for when the tableview is populated.

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

    static NSString *MyIdentifier = @"MyIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
    }

    // Set up the cell  
    int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];   
    NSString *temp = [[NSString alloc] initWithString:[[stories objectAtIndex: storyIndex] objectForKey: @"title"]];
    cell.textLabel.text = temp;
    [[cell textLabel] setTextColor:[UIColor colorWithRed:154.0/255.0 green:14.0/255.0 blue:2.0/255.0 alpha:1]];
    [[cell textLabel] setFont:[UIFont systemFontOfSize:12.0]];  

    return cell;
}

For the life of me I don't know why it won't change the font! Also the above code works fine if I hard code in what the cell text is such as cell.textLabel.text = @"TEST";

Any suggestions? Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First, you should autorelease your cell. You are leaking memory like crazy presently.

Second, you should update the font in tableView:willDisplayCellForRow:atIndexPath:. If you are using a standard table view, it will make changes to your cells (at random times) and you will need to do things like font changes, background color, etc in the tableView:willDisplayCellForRow:atIndexPath: instead of in the data source method.

See also this thread: What is -[UITableViewDelegate willDisplayCell:forRowAtIndexPath:] for?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...