gpt4 book ai didi

ios - 使用tableview在文本字段上方显示最后一条短信

转载 作者:行者123 更新时间:2023-11-29 12:13:32 25 4
gpt4 key购买 nike

我正在尝试在 ios 中使用 tableview 和 textfield 创建一个聊天应用程序,但我希望在 uibutton 的单击事件中最后一行的 tableview 最后一行的文本字段上方看到最后发送的消息。如何做到这一点,请帮助我。

-(IBAction)sendmessage:(id)sender
{
NSError *error;
NSFileManager *filemanager = [NSFileManager defaultManager];
NSMutableDictionary *messageDictionary = [NSMutableDictionary new];
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *messageString = [array objectAtIndex:0];
NSString *datallist =[messageString stringByAppendingPathComponent:@"message.plist"];

if(messagearray == Nil)
{
messagearray = [[NSMutableArray alloc]init];
}
NSInteger contactid = 0;
if(datallist)
{
BOOL isFileExist = [filemanager fileExistsAtPath:datallist];
if(isFileExist)
{
messagearray = [NSMutableArray arrayWithContentsOfFile:datallist];


}
}
if(messagearray.count >0)
{
NSDictionary *dic = [messagearray objectAtIndex:(messagearray.count - 1)];
NSInteger val = [[dic objectForKey:@"id"] integerValue];
contactid = val+1;
}
else{

contactid = 1;
}

[messageDictionary setObject:[NSNumber numberWithInteger:contactid] forKey:@"id"];
[messageDictionary setObject:messageText.text forKey:@"id"];

[messagearray addObject:messageDictionary];
NSData *data = [NSPropertyListSerialization dataFromPropertyList:messagearray format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
if(data)
{
[data writeToFile:datallist atomically:YES];

[messagingtable reloadData];
}
else {
NSLog(@"Error in save data: %@",error);

}


}

最佳答案

尝试 scrollToRowAtIndexPath:atScrollPosition:animated: UITableView 的方法

Scrolls through the table view until a row identified by index path is at a particular location on the screen.

objective-c

- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath
atScrollPosition:(UITableViewScrollPosition)scrollPosition
animated:(BOOL)animated;

swift

func scrollToRowAtIndexPath(_ indexPath: NSIndexPath,
atScrollPosition scrollPosition: UITableViewScrollPosition,
animated animated: Bool)

参数

indexPath An index path that identifies a row in the table view by its row index and its section index.

NSNotFound is a valid row index for scrolling to a section with zero rows.

scrollPosition A constant that identifies a relative position in the table view (top, middle, bottom) for row when scrolling concludes. See Table View Scroll Position for descriptions of valid constants.

animated YES if you want to animate the change in position; NO if it should be immediate.

已编辑

-(IBAction)sendmessage:(id)sender
{
...............
if(data)
{
[data writeToFile:datallist atomically:YES];
[messagingtable reloadData];
// here, you need to scroll your table
[messagingtable scrollToRowAtIndexPath:[NSIndexPath indexPathForRow: messagearray.count-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
else {
NSLog(@"Error in save data: %@",error);
}
}

引用:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/index.html#//apple_ref/occ/instm/UITableView/scrollToRowAtIndexPath:atScrollPosition:animated :

关于ios - 使用tableview在文本字段上方显示最后一条短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32601089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com