-3

I know this is simple, but I can't find a simple answer! I have a regular UITableView and I just added a text view to a custom cell. I'm not sure how to access this textview in cellForRowAtIndexPath. I tried making it an iboutlet, but it doesn't show up when I do cell.??!?!?!?. The reuse is set also. Any ideas?

Edit: I also did all the connections in ib

In the .h file...

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

In the .m file...

@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__);
}

The problem is I should be able to do cell.myTextView, but it's not there

jgvb
  • 304
  • 5
  • 16
  • Do I need to add an extra class just for this custom cell? – jgvb Jan 09 '14 at 04:08
  • 1
    I'm pretty sure there are many answers here about what you're trying to do. If you insist that no other questions cover what you're trying to do, please add some code snippets so that we can help you. – phi Jan 09 '14 at 04:09
  • you should make object of your custom cell not a UITableViewCell object. – Sunny Shah Jan 09 '14 at 04:16
  • You textView is not a part of UITableViewCell. If you only want to use a text label you can get it. cell.textLabel. – Rashad Jan 09 '14 at 04:17
  • @Sunnyshah So I need to make a new class for that custom cell? – jgvb Jan 09 '14 at 04:18
  • @Rashad yeah, the problem is I want to use textview, so I made a custom cell in Interface Builder with a textview in it. But I don't know how to access that textView from the code in the "cellForRowAtIndexPath" function – jgvb Jan 09 '14 at 04:20
  • obviously you need to make class of UITableViewCell – Sunny Shah Jan 09 '14 at 04:20
  • @Sunnyshah please explain why it's obvious – jgvb Jan 09 '14 at 04:22
  • possible duplicate of [How do you load custom UITableViewCells from Xib files?](http://stackoverflow.com/questions/540345/how-do-you-load-custom-uitableviewcells-from-xib-files) – Aaron Brager Jan 09 '14 at 04:27

3 Answers3

1

For Example

    static   NSString *cellIdentifier= @"Cell";
      CustomCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell=[[[NSBundle mainBundle]loadNibNamed:@"CustomCell" owner:self options:nil]objectAtIndex:0];
        }
cell.myTextView.text=@"your text";
Sunny Shah
  • 12,990
  • 9
  • 50
  • 86
0

May be this is what you want.

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

Edit: For custom cell In .h file

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

@end

In .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

In CellForRowAtIndexPath

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

Hope this helps... :)

Rashad
  • 11,057
  • 4
  • 45
  • 73
0

Do like this,

YourCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[YourCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell. myTextView.text = @"My TextView";
return cell;
Venk
  • 5,949
  • 9
  • 41
  • 52