如何将 UITextView 添加为 NSMutableArray 中的对象

发布于 2024-12-19 15:17:01 字数 1672 浏览 1 评论 0原文

我正在尝试将 uitextview 添加为 uitabaleview 单元格的子视图,为此我正在 cellForRowAtIndex 中以编程方式创建 uitextview,并且我希望它能够动态显示从 nsmutablearray 到 uitextview 的文本,但是问题是...如何区分特定的不同 uitextview uitableview 的单元格。我的代码是这样的......

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

    static NSString *CellIdentifier=@"cell";
    UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)

 {

        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    @try{

    // Set up the cell...

    if (tv == self.smsTableView) {

        int count1=[smsTxt count];

        int rowCount=indexPath.row;

    int index1=(count1-(rowCount+1));

        NSLog(@"count:::%d",count1);

        NSLog(@"row count:::%d",rowCount);

        NSString *cellValueSMSTxt = [self.smsTxt objectAtIndex:index1];

        UITextView *msgView=[[UITextView alloc]init];

        msgView.frame=CGRectMake(12, 15, 280, 45);
        msgView.font=[UIFont systemFontOfSize:12.0];
        msgView.editable=FALSE;
        msgView.textColor=[UIColor grayColor];
        msgView.backgroundColor=[UIColor clearColor];
        msgView.text=cellValueSMSTxt;
        arrMsgView=[[NSMutableArray alloc]init];
        [arrMsgView addObject:msgView];
        [msgView release];
        UITextView *tempTextView=[arrMsgView objectAtIndex:rowCount];
        NSLog(@"countforarr:::%d",[arrMsgView count]);
        [cell.contentView addSubview:tempTextView];
        [arrMsgView release];

    }
    }@catch (NSException *e) {
        NSLog(@"%@",e);


    }

i am trying to add uitextview as subview of uitabaleview's cell and for that i am creating uitextview programatically in cellForRowAtIndex and i want it to be display the text dynamically from the nsmutablearray to uitextview however problem is ... how to differentiate for different uitextview of particular uitableview's cell. my code is in this way.......

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

    static NSString *CellIdentifier=@"cell";
    UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)

 {

        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    @try{

    // Set up the cell...

    if (tv == self.smsTableView) {

        int count1=[smsTxt count];

        int rowCount=indexPath.row;

    int index1=(count1-(rowCount+1));

        NSLog(@"count:::%d",count1);

        NSLog(@"row count:::%d",rowCount);

        NSString *cellValueSMSTxt = [self.smsTxt objectAtIndex:index1];

        UITextView *msgView=[[UITextView alloc]init];

        msgView.frame=CGRectMake(12, 15, 280, 45);
        msgView.font=[UIFont systemFontOfSize:12.0];
        msgView.editable=FALSE;
        msgView.textColor=[UIColor grayColor];
        msgView.backgroundColor=[UIColor clearColor];
        msgView.text=cellValueSMSTxt;
        arrMsgView=[[NSMutableArray alloc]init];
        [arrMsgView addObject:msgView];
        [msgView release];
        UITextView *tempTextView=[arrMsgView objectAtIndex:rowCount];
        NSLog(@"countforarr:::%d",[arrMsgView count]);
        [cell.contentView addSubview:tempTextView];
        [arrMsgView release];

    }
    }@catch (NSException *e) {
        NSLog(@"%@",e);


    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欢烬 2024-12-26 15:17:01

您可以通过子类化 UITableViewCell 并保留指向不同 UITextView 的指针,或者通过在 UITextView 上设置标签(标签是 UIView 属性)来区分:

@property(nonatomic) NSInteger tag

现在您正在创建一个数组来保存 UITextView 并销毁它,这不会不会让你走得很远。

 arrMsgView=[[NSMutableArray alloc]init];
 [arrMsgView addObject:msgView];
 [msgView release];
 UITextView *tempTextView=[arrMsgView objectAtIndex:rowCount];
 NSLog(@"countforarr:::%d",[arrMsgView count]);
 [cell.contentView addSubview:tempTextView];
 [arrMsgView release];

要访问给定的 UITextView,您可以做的是循环遍历 contentView 子视图来查找给定的对象:

for (UIView* v in [contentView subviews]) {
    if ([v isKindOfClass:[UITextView class]] && v.tag == someIdTag) {
        // do something
    }
}

在这种情况下,您根本不需要额外的数组(子视图对象是一个数组)。

You can differentiate by subclassing UITableViewCell and keeping pointers to the different UITextView, or by setting a tag on the UITextView (tag is a UIView property):

@property(nonatomic) NSInteger tag

As it is now you are creating an array to hold the UITextViews and destroying it, which doesn't get you very far.

 arrMsgView=[[NSMutableArray alloc]init];
 [arrMsgView addObject:msgView];
 [msgView release];
 UITextView *tempTextView=[arrMsgView objectAtIndex:rowCount];
 NSLog(@"countforarr:::%d",[arrMsgView count]);
 [cell.contentView addSubview:tempTextView];
 [arrMsgView release];

What you could do to access a given UITextView is loop through the contentView subviews looking for a given object:

for (UIView* v in [contentView subviews]) {
    if ([v isKindOfClass:[UITextView class]] && v.tag == someIdTag) {
        // do something
    }
}

in which case you wouldn't need an additional array at all (the subviews object is an array).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文