2014-01-09 32 views
-3

我知道這很簡單,但我找不到一個簡單的答案!我有一個普通的UITableView,我只是給一個自定義單元格添加了一個文本視圖。我不知道如何訪問cellForRowAtIndexPath中的這個textview。我試過把它製作成iboutlet,但是當我做單元格時它不會顯示出來。?!?!?!?。重用也被設置。有任何想法嗎?如何更改自定義單元格上的屬性?

編輯:我也沒有在IB

在.h文件中所有的連接...

@property(nonatomic, strong) IBOutlet UITextView *myTextView; 

在.m文件...

@synthesize myTextView = _myTextView; 
    static NSString *CellIdentifier = @"all_table_reuse_identifier"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
if (nil == cell) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    NSLog(@"%s", __PRETTY_FUNCTION__); 
} 

問題是我應該可以做cell.myTextView,但它不存在

+0

我是否需要添加一個額外的類只是這個自定義單元格? – jgvb

+1

我很確定這裏有很多關於你想要做什麼的答案。如果您堅持沒有其他問題涉及您想要做的事情,請添加一些代碼片段,以便我們可以爲您提供幫助。 – phi

+0

@Irene也許更好? – jgvb

回答

1

對於實例

static NSString *cellIdentifier= @"Cell"; 
     CustomCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
     if (cell == nil) { 
      cell=[[[NSBundle mainBundle]loadNibNamed:@"CustomCell" owner:self options:nil]objectAtIndex:0]; 
     } 
[email protected]"your text"; 
0

可能是這是你想要的。

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 
cell.textLabel.text = @"Why so serious?"; 
return cell; 

編輯:對於自定義單元格 在.h文件中

#import <UIKit/UIKit.h> 
@interface TestCell : UITableViewCell 
@property (weak, nonatomic) IBOutlet UITextView *tv; 

@end 

在.M

#import "TestCell.h" 

@implementation TestCell 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 
-(void) setFrame:(CGRect)frame 
{ 
    float inset = 20.0f; 
    frame.origin.x += inset; 
    frame.size.width -= 2 * inset; 
    [super setFrame:frame]; 
} 
- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 
} 

@end 

在的cellForRowAtIndexPath

TestCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[TestCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 
cell.tv.text = @"This is textview"; 
return cell; 

希望這有助於...: )

0

做這樣的,

YourCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[YourCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 
cell. myTextView.text = @"My TextView"; 
return cell; 
相關問題