问题
have a UITableViewCell, to which I add a label on it with tag 1000.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChecklistItem"];
UILabel *label = (UILabel *) [cell viewWithTag:1000];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
then
UILabel *labelName = (UILabel *)[cell viewWithTag:1000]
labelName.text = @"test".
When I run, the cell is empty. Any advice?
回答1:
You should check if [tableView dequeueReusableCellWithIdentifier:@"ChecklistItem"]
and [cell viewWithTag:5000]
are returning something other than nil.
If they are, be sure to do it in this order:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//Try to reuse the cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChecklistItem"];
//If you can't reuse, instantiate the cell
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ChecklistItem"];
}
//Now that you have your cell, find the label
UILabel *labelName = (UILabel *)[cell viewWithTag:5000];
//Check if the label exists just in case
if (labelName) {
//And set the text
[labelName setText:@"test"];
}
}
回答2:
The problem could also be that you aren't using a unique tag number. Meaning that something in your view controller scene could possibly have the same tag number that you have defined on another object.
For example, for me, I had a UITableViewCell which had a tag # of 0 and a UILabel with a tag # of 0 and another UILabel with a tag # of 1. This caused problems and made it to where tag #1 wouldn't display at all. I could see the text when I clicked on the row -- but the data wouldn't show when the row wasn't clicked on.
When I changed the tag numbers of both UILabels, it fixed the issue.
回答3:
The issue already solved, my application was working very fine on iPhone, so I decide to make it universal. I copy all view controller to iPad storyboard. And I start doing some change on iPad storyboard.
Whatever I add/delete on iPad storyboard, didn't make any change as the main interface was iphone and the reason was that I didn't change the main interface for iPad, so while running on iPad, I was using the storyboard of iPhone.
来源:https://stackoverflow.com/questions/25888469/uilabel-with-viewwithtag-text-not-appearing