1

UITextField を UITableView セルに合わせる最良の方法は何だろうと思っています。

私は以前にこの方法を使用しました:

@implementation UITextField (custom)
- (CGRect)textRectForBounds:(CGRect)bounds {
    return CGRectMake(bounds.origin.x + 0, bounds.origin.y + 10,
                      bounds.size.width - 30, bounds.size.height - 16);
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
    return [self textRectForBounds:bounds];
}
@end

しかし、これは問題を引き起こします:

Category is implementing a method which will also be implemented by its primary class

これらの警告を非表示にする方法を見てきましたが、これは正しい方法というよりハックのように感じます。

UItextField をセルに合わせる最良の方法は何ですか? これは私の分野です:

// Username field
usernameField = [[UITextField alloc] initWithFrame:(CGRectMake(10, 0, 300, 43))];
usernameField.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

次のようにセルに入力されます。

if (indexPath.row == 0) {
        [cell.contentView addSubview:usernameField];
    } 

画像: ここに画像の説明を入力

4

1 に答える 1

0

カスタム UITableViewCell の layoutSubviews メソッドをオーバーライドして、これを行うことをお勧めします。はるかに簡単です。何かのようなもの:

    - (void)layoutSubviews
    {
        [super layoutSubviews];
        float  w, h;

        w = (int)(self.frame.size.width - 30);          
            h = (int)(self.frame.size.height - 16);

        [self.detailTextLabel setFrame:CGRectMake(0, 10, w, h)];
    }

カスタム セルの初期化の一部を次に示します。

    @interface MyCustomCell: UITableViewCell {

    }
    @end

    @implementation MyCustomCell


    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
            }
            return self;
    }

カスタム セルが作成される場所の一部を次に示します。

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

        static NSString *CellIdentifier = @"Cell";

        MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[MyCustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        }
于 2013-02-25T22:37:33.730 に答える