2013-09-02 1 views
0

я добавил UIView к пользовательской ячейке UITableView в раскадровке, и я к нему доступ, как это:UITableViewCell viewWithTag получить кадр UIView неприятности

UIView *customView = (UIView *)[cell viewWithTag:CELL_CUSTOM_VIEW_TAG]; 
NSLog(@"%f", customView.frame.size.width); 

Но я его ширину 0.0000. Я обращаюсь к нему неправильно?

EDIT: Вот мой метод полной cellForRow:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"CustomCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    UIImageView *customImageView = (UIImageView *)[cell viewWithTag:CELL_IMAGE_VIEW_TAG]; 
    customImageView.image = [UIImage imageNamed:@"foo.jpg"]; 
    //down is the code why I need the frame of it 
    //I'm trying to make a right border to this UIImageView: 
    CGRect imageViewFrame = customImageView.frame; 
    CALayer *imageViewRightBorder = [self rightBorderWithColour:[UIColor blackColor] forView:imageViewFrame]; 
    [customImageView.layer addSublayer:imageViewRightBorder]; 

    return cell; 
} 

А вот мой метод для правой границы до ImageView:

- (CALayer *)rightBorderWithColour:(UIColor *)color forView:(CGRect)viewFrame { 
    CALayer *rightBorder = [CALayer layer]; 
    rightBorder.frame = CGRectMake(viewFrame.size.width, 0.0f, 1.0f, viewFrame.size.height); 
    rightBorder.backgroundColor = [color colorWithAlphaComponent:1.0f].CGColor; 
    return rightBorder; 
} 
+0

Где вы установили рамку из uiview? – Woodstock

+0

в раскадровке –

+0

@AlickDikan Проверено ли 'customView'' nil'? Может быть, тег установлен неправильно? – Amar

ответ

2

Я не понимаю вашу логику. cellForRowAtIndexPath - это то, где вы определяете UIView так, что у вас это есть. Как сказал другой пользователь, покажите нам свой код.

В любом случае, именно так я определил пользовательский UIButton в своем представлении таблицы, который отображается в каждой строке. Я предполагаю, что вы пытаетесь сделать что-то подобное. Забудьте добавить свой UIView в раскадровку. Посмотрите, можете ли вы воспроизвести его с помощью этого кода. Конечно, измените его на код UIView.

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

NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row]; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell == nil) 
    { 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 

     button = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [button addTarget:self 
        action:@selector(timerStopStart:) 
     forControlEvents:UIControlEventTouchDown]; 
     [button setTitle:@"Start/Stop" forState:UIControlStateNormal]; 
     button.frame = CGRectMake(5.0f, 40.0f, 72.0f, 77.0f); 
     button.tag = (indexPath.row)+100; 
     [cell addSubview:button]; 

     //NSLog(@"number: %d ...", (indexPath.row)+100); 
    } 
} 
+0

Я делаю это, потому что у меня есть пользовательский UITableViewCell в раскадровке, где я, например, добавил UIButton (потому что я просто не хочу делать это программно). Я извлекаю его, используя cellviewWithTag. Это моя логика –

+0

Для удовольствия попробуйте добавить UIView или UIButton (все, что вы пытаетесь добавить) в таблицу программно. Бьюсь об заклад, вы доберетесь до него. Поверьте мне, я был на этом пути раньше. –

+0

Нет необходимости добавлять 'UIView' программно. Покажите нам код, я думаю, вы сделали небольшую ошибку, мы можем указать вам на ошибку. – Bhavin