Một cách khác là tạo ra một tế bào tùy chỉnh và ghi đè layoutSubviews
phương pháp.
@interface CustomCell : UITableViewCell
@end
@implementation CustomCell
- (void)layoutSubviews
{
[super layoutSubviews];
// grab bound for contentView
CGRect contentViewBound = self.contentView.bounds;
// grab the frame for the imageView
CGRect imageViewFrame = self.imageView.frame;
// change x position
imageViewFrame.origin.x = contentViewBound.size.width - imageViewFrame.size.width;
// assign the new frame
self.imageView.frame = imageViewFrame;
}
@end
Rembember rằng trong cellForRowAtIndexPath
bạn cần để tạo ra và tái sử dụng CustomCell
và không UITableViewCell
.
Hy vọng điều đó sẽ hữu ích.
Sửa
#import "CustomCell.h"
// other code here...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomCell";
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
return cell;
}
Nguồn
2012-02-25 16:48:36
Tôi sợ bạn không thể với standar 'UITableViewCell' bạn phải sử dụng ô của riêng bạn rồi tùy chỉnh nó. – HelmiB