我在Interface Builder中創建了一個原型單元。這個原型包含一個imageview,它應該根據字符串的長度改變大小。 因此,我在我的UITableViewCell的子類中編寫了一個方法來計算給定字體的文本字符串的寬度,並將此寬度設置爲imageview的寬度。 這工作正常,直到我推到另一個視圖控制器,然後回到我的表視圖。然後視圖重新定位到右邊20個像素。UITableViewCell子類中的標籤更改位置
有誰知道爲什麼會發生這種情況?如果我將x位置設置爲靜態,例如200.
更新1:
在我- (UITableViewCell *)tableView:cellForRowAtIndexPath:
我用我的UITableViewCell,DFolderCell
的子類。這就調用方法- (void)updateIndicatorsCount:andStarred
,它設置count
和starred
標籤的值,並使這些標籤和圖像countIcon
和starredIcon
的寬度變爲帶有給定字體的_count
和_starred
的寬度。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"folderCell";
DFolderCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[DFolderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
int index = indexPath.row;
Folder *folder = (Folder *) [folders objectAtIndex:index];
FolderIcon icon = (FolderIcon) folder.icon.intValue;
cell.name.text = folder.name;
cell.icon.image = [[DFolders sharedFolders] imageForFolderIcon:icon];
int count = [[DDrafts sharedDrafts] amountOfDraftsInFolder:folder withType:DraftLoadTypeRecent];
int starred = [[DDrafts sharedDrafts] amountOfDraftsInFolder:folder withType:DraftLoadTypeStarred];
[cell updateIndicatorsCount:count andStarred:starred];
return cell;
}
下面的方法是我的子類DFolderCell
/* Update size of count and starred indicators */
- (void)updateIndicatorsCount:(int)_count andStarred:(int)_starred
{
UIFont *badgeFont = [UIFont systemFontOfSize:11.0];
UIImage *background;
_count = 1000; // Test
if (_count > 0)
{
self.count.text = [NSString stringWithFormat:@"%i", _count];
CGSize size = [self.count.text sizeWithFont:badgeFont];
CGRect frame = self.countIcon.frame;
frame.size.width = size.width;
frame.origin.x = 320 - 20 - size.width;
self.countIcon.frame = frame;
self.count.frame = frame;
background = [UIImage imageNamed:@"Folders_Badge_Count.png"];
self.countIcon.image = [background stretchableImageWithLeftCapWidth:2 topCapHeight:2];
self.count.hidden = NO;
self.countIcon.hidden = NO;
}
/* Code for _starred is similar to code for _count */
}
的一部分,這是結果。
這是怎麼回事應該總是尋找(並選擇一個小區前的樣子)
當細胞已被選中,一個新的視圖控制器已經被推到導航堆棧和我有通過返回來彈出此視圖控制器,這是它的外觀。
更新2:
我已經保持在細胞原型的ImageView但評論出代碼,其尺寸和圖像設置爲圖像伸縮。我還將標籤的背景顏色更改爲純紅色,以查看標籤的確切尺寸。大小似乎是正確的。
這是我現在的- (void)updateIndicatorsCount:andStarred:
方法。
- (void)updateIndicatorsCount:(int)_count andStarred:(int)_starred
{
UIFont *badgeFont = [UIFont systemFontOfSize:11.0];
// UIImage *background;
_count = 1000;
if (_count > 0)
{
self.count.text = [NSString stringWithFormat:@"%i", _count];
CGSize size = [self.count.text sizeWithFont:badgeFont];
CGRect frame = self.countIcon.frame;
frame.size.width = size.width;
frame.origin.x = 320 - 30 - size.width;
self.count.frame = frame;
self.count.hidden = NO;
self.count.backgroundColor = [UIColor redColor];
/* DON'T DO ANYTHING TO THE IMAGE */
/*
background = [UIImage imageNamed:@"Folders_Badge_Count.png"];
self.countIcon.hidden = NO;
self.countIcon.image = [background stretchableImageWithLeftCapWidth:2 topCapHeight:2];
self.countIcon.frame = frame;
*/
}
}
前推到另一個視圖控制器:
從視圖控制器回去之後:
你可以在這裏顯示代碼嗎?它在做什麼方法? – Jim
屏幕截圖可能也有幫助。 – occulus
@Jim我已經更新了顯示代碼的答案。 – simonbs